Fragment won't launch

故事扮演 提交于 2019-12-02 16:01:02

问题


I have onclicklistener that works. I am trying to launch a new fragment from the button click on the list view. Right now, the fragment does not launch. However, the emulator we are using does not crash, so if we're understanding this correctly, it isn't connecting to the new fragment/XML page.

  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());

}

Here is our XML code for the view that we are trying to add the fragment over top of.

<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>

回答1:


change this line

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

to:

fragmentTransaction.commit;

and better replace fragment in container ,

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



回答2:


You should be using commit() to start your transaction, not an Intent(), you also want to use replace() not 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();
}


来源:https://stackoverflow.com/questions/43107980/fragment-wont-launch

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!