我有主要布局。我想在主LinearLayout中添加片段。但是如果我想在主片段中添加子片段,它当前存在且可见。
<LinearLayout
android:id="@+id/linearlayout_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/footer"
android:layout_below="@id/header"
android:orientation="vertical" >
</LinearLayout>
在Fragment活动中,我添加了这个:
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
HomeFragment homeFragment = HomeFragment.newInstance();
homeFragment.setRetainInstance(true);
fragmentTransaction.replace(R.id.linearlayout_fragment, homeFragment);
fragmentTransaction.commit();
现在我想在当前主页片段中添加子片段:
FragmentManager fragmentManager = getChildFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
RepairFragment repairFragment = RepairFragment.newInstance();
repairFragment.setRetainInstance(true);
fragmentTransaction.add(R.id.linearlayout_fragment, repairFragment);
fragmentTransaction.commit();
但我得到了例外:
04-15 11:18:26.394: E/AndroidRuntime(12561): FATAL EXCEPTION: main
04-15 11:18:26.394: E/AndroidRuntime(12561): java.lang.IllegalArgumentException: No view found for id 0x7f070005 (com.test.simple:id/linearlayout_fragment) for fragment RepairFragment{42319fb0 #0 id=0x7f070005}
04-15 11:18:26.394: E/AndroidRuntime(12561): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:919)
04-15 11:18:26.394: E/AndroidRuntime(12561): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
04-15 11:18:26.394: E/AndroidRuntime(12561): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
04-15 11:18:26.394: E/AndroidRuntime(12561): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1467)
04-15 11:18:26.394: E/AndroidRuntime(12561): at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:440)
04-15 11:18:26.394: E/AndroidRuntime(12561): at android.os.Handler.handleCallback(Handler.java:730)
04-15 11:18:26.394: E/AndroidRuntime(12561): at android.os.Handler.dispatchMessage(Handler.java:92)
04-15 11:18:26.394: E/AndroidRuntime(12561): at android.os.Looper.loop(Looper.java:176)
04-15 11:18:26.394: E/AndroidRuntime(12561): at android.app.ActivityThread.main(ActivityThread.java:5493)
04-15 11:18:26.394: E/AndroidRuntime(12561): at java.lang.reflect.Method.invokeNative(Native Method)
04-15 11:18:26.394: E/AndroidRuntime(12561): at java.lang.reflect.Method.invoke(Method.java:525)
04-15 11:18:26.394: E/AndroidRuntime(12561): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1225)
04-15 11:18:26.394: E/AndroidRuntime(12561): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1041)
04-15 11:18:26.394: E/AndroidRuntime(12561): at dalvik.system.NativeStart.main(Native Method)
我试过了:
fragmentTransaction.replace(getParentFragment().getId(), repairFragment);
fragmentTransaction.replace(getView().getId(), repairFragment);
答案 0 :(得分:0)
所以试试另一个xml
<FrameLayout
android:id="@+id/linearlayout_fragment"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="@+id/footer"
android:layout_below="@id/header" >
</FrameLayout>
答案 1 :(得分:0)
解决了。刚刚将id分配给父片段布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linearlayout_repair_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/textview_repair"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="repair" />
<TextView
android:id="@+id/textview_repair_1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="repair 1 " />
</LinearLayout>
和
fragmentTransaction.replace(R.id.linearlayout_repair_fragment, repairFragment);