片段无法启动

时间:2017-03-30 03:27:39

标签: android xml android-fragments onclick

我有onclicklistener可行。我试图从列表视图上的按钮单击启动一个新片段。现在,片段没有启动。但是,我们使用的模拟器不会崩溃,所以如果我们正确理解它,它就不会连接到新的片段/ XML页面。

  public void onItemClick(AdapterView<?> adapterView, View view, final int i, long l)
{
    myBadData.setId(i);
    Fragment fr = new event_description();
    //fr.setArguments(bundle);
    FragmentManager fm = getFragmentManager();
    FragmentTransaction fragmentTransaction = fm.beginTransaction();
    //int contId = v.getId();
    fragmentTransaction.add(R.id.page, fr);
   // fragmentTransaction.add(view.getId(), fr);
   // fragmentTransaction.commit();

    Intent intent =new Intent(eventList.this, fr.getClass());

}

以下是我们正在尝试将片段添加到视图之上的视图的XML代码。

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:id="@+id/page"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 tools:context="com.example.alex.qtapandroid.ui.fragments.DayFragment">

<!-- TODO: Update blank fragment layout -->



    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" />

    <ListView
        android:id="@+id/dayList"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

 </FrameLayout>

2 个答案:

答案 0 :(得分:0)

更改此行

Intent intent =new Intent(eventList.this, fr.getClass());

到:

fragmentTransaction.commit;

并更好地替换容器中的片段,

fragmentTransaction.replace(R.id.page, fr);

答案 1 :(得分:0)

您应该使用commit()来开始交易,而不是Intent(),您还想使用replace()而不是add()

 public void onItemClick(AdapterView<?> adapterView, View view, final int i, long l)
{
    //Assuming that this creates a new fragment
    Fragment fr = new event_description();
    FragmentManager fm = getFragmentManager();
    FragmentTransaction fragmentTransaction = fm.beginTransaction();
    fragmentTransaction.replace(R.id.page, fr, "TAG ID");
    fragmentTransaction.commit();
}
相关问题