无法加载片段

时间:2017-09-11 10:40:42

标签: android fragment

嗨,我在这一行有问题,我不知道为什么

FragmentManager manager = getFragmentManager();
        FragmentTransaction trans = manager.beginTransaction();
        trans.replace(R.id.containerr,new FragmentLastComments());
        trans.commit();

XML

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/containerr"
    android:layoutDirection="ltr">

</RelativeLayout>

片段xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layoutDirection="ltr">


    <LinearLayout
        android:id="@+id/linearLayout3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_marginTop="55dp"
        android:orientation="vertical">


        <TextView
            android:id="@+id/txt_lastcomments"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:gravity="center"
            android:textSize="20dp"
            android:layout_marginBottom="10dp"
            android:text="last comments" />

        <android.support.v7.widget.RecyclerView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginRight="5dp"
            android:layout_marginLeft="5dp"
            android:id="@+id/rv_lastcomments"/>


    </LinearLayout>




</RelativeLayout>

此行无效

trans.replace(R.id.containerr,new FragmentLastComments());

当我删除此行时,我没有遇到任何问题,但是当我使用此行时,我遇到问题我认为这里存在问题

R.id.containerr

我希望有人能帮我解决这个问题

3 个答案:

答案 0 :(得分:0)

试试这种方式,

FragmentManager mFragmentManager=getFragmentManager();
Fragment mfragment = new FragmentLastComments();
mFragmentManager
         .beginTransaction()
         .replace(R.id.rl_main_new, fragmentDash, "tag")
         .commit();

我希望这会对你有所帮助。

答案 1 :(得分:0)

    FragmentManager fragmentManager = getSupportFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    fragmentTransaction.add(R.id.frame_aaron, fragment);
    fragmentTransaction.addToBackStack(null);
    fragmentTransaction.commit();

答案 2 :(得分:0)

将你的片段放在FrameLayout中,如下所示:

  <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/containerr"
android:layoutDirection="ltr">

                    <FrameLayout
                        android:id="@+id/fragment_container"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"/>

</RelativeLayout>

用这个改变你的Fragment方法:

FragmentManager manager = getFragmentManager();
    FragmentTransaction trans = manager.beginTransaction();
    trans.replace(R.id.fragment_container,new FragmentLastComments());
    trans.commit();