0

我有一个活动,它有一个列表视图,它使用列表适配器来显示元素。当在片段上单击具有文本的元素时,应创建具有播放按钮、搜索栏和媒体播放器的片段。但是,当我的活动被调用时,我得到了这个错误。

第 14 行是 list_songs 是具有“片段”开始标签的列表

> 02-26 02:01:27.625: E/AndroidRuntime(2419): FATAL EXCEPTION: main
> 02-26 02:01:27.625: E/AndroidRuntime(2419):
> java.lang.RuntimeException: Unable to start activity
> ComponentInfo{com.songs/com.songs.display.DisplaysongDetails}:
> android.view.InflateException: Binary XML file line #14: Error
> inflating class fragment

活动的代码是

public class DisplaysongDetails extends ListActivity {
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.setContentView(R.layout.list_songs);
    setListAdapter(new ArrayAdapter<String>(this, R.layout.row,songDetails));
    ListView listView = (ListView) this.findViewById(android.R.id.list);//getListView();
    listView.setTextFilterEnabled(true);
    listView.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
            String name = ((TextView) view).getText().toString();
            if (name=="Play")
            {
                PlayerFragment frag = (PlayerFragment) getFragmentManager().findFragmentById(R.id.player_fragment);
                if (frag == null) {
                    FragmentTransaction ft = getFragmentManager().beginTransaction();
                    ft.add(R.id.player_fragment, new PlayerFragment());
                    ft.commit(); 
                }}
        }
    });

}}

list_songs.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FFFFFF">
<ListView
    android:id="@android:id/list"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" />   
<fragment
    android:id="@+id/player_fragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    class="com.songs.PlayerFragment" />     
</LinearLayout>

片段类是:

public class PlayerFragment extends Fragment implements  OnTouchListener, OnCompletionListener, OnBufferingUpdateListener{

private Button btn_play;
private SeekBar seekBar;
private MediaPlayer mediaPlayer;
private int lengthOfAudio;
private int length=0;
String URL = (new AppConfig()).getURL();
private final String url = URL + "/play/song.mp3";
private final Handler handler = new Handler();
private final Runnable r = new Runnable() { 
    public void run() {
        updateSeekProgress();                   
    }
};

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

@SuppressLint("NewApi")
@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.player_fragment, container, false);
    btn_play = (Button) view.findViewById(R.id.btn_play);
    btn_play.setOnClickListener(new OnClickListener());
    seekBar = (SeekBar)getActivity().findViewById(R.id.seekBar);
    seekBar.setOnTouchListener(this);

    mediaPlayer = new MediaPlayer();
    mediaPlayer.setOnBufferingUpdateListener(this);
    mediaPlayer.setOnCompletionListener(this);

    return view;
}}

片段 XML

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<SeekBar
android:id="@+id/seekBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="3dp" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:gravity="center_horizontal"
android:orientation="horizontal" >
<Button
    android:id="@+id/btn_play"
    android:layout_width="70dp"
    android:layout_height="wrap_content"
    android:text="play" />
</LinearLayout>
</LinearLayout>
4

2 回答 2

1

另一个问题:

seekBar = (SeekBar)getActivity().findViewById(R.id.seekBar);
seekBar.setOnTouchListener(this);

seekBar仅定义为 Fragment 布局的一部分,而不是 Activity 的。尝试添加侦听器时,该调用getActivity().findViewById()应返回并导致 NPE。null但是,需要更多信息(来自堆栈跟踪)来查看是否有其他问题(除了这个和字符串比较)。

什么是有效的:

seekBar = (SeekBar)view.findViewById(R.id.seekBar);
seekBar.setOnTouchListener(this);
于 2013-02-27T04:34:39.933 回答
1

首先,你已经给出fill_parent了你的宽度和高度,ListView这使得你的 ListView 占据了整个父级(这里LinearLayout),所以没有Fragment. null您检查了您的代码很好if (frag == null),但是您再次尝试将您的片段添加到View不在屏幕中的相同片段(未附加)。

如果您发现您Fragment的未附加到您的视图层次结构,也许您会想要开始一个新活动。

if (frag == null) {
    Intent playerActivity = new Intent(DisplaysongDetails.this, PlayerActivity.class)
    startActivity(playerActivity);
    }

现在,创建一个新的Activity被调用PlayerActivity并添加PlayerFragment它的布局。您可以使用 传递任何项目数据Extras

于 2013-02-27T04:51:30.560 回答