我正在项目中使用带有3个片段的ViewPager。这三个片段中的一个必须包含其中的另一个片段,因为我在片段的布局中定义了一个FrameLayout:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:id="@+id/valuesContainer"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp" >
...
</LinearLayout>
<FrameLayout
android:id="@+id/model3d_container"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="@+id/valuesContainer"
android:layout_centerHorizontal="true"
android:layout_margin="15dp"
android:layout_marginTop="20dp" />
</RelativeLayout>
现在我以编程方式添加新片段:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.layout_row3d, container, false);
/*Check if fragment is already loaded*/
Fragment frag = getActivity().getSupportFragmentManager().findFragmentById(R.id.model3d_container);
if (frag == null) {
/*load fragment with 3d model*/
Fragment modelFragment = new Obj3DView();
FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
transaction.add(R.id.model3d_container, modelFragment).commit();
}
return view;
}
但是当我浏览ViewPager的视图时,当我返回包含子片段的片段时,应用程序崩溃了。阅读日志,似乎与子片段加载有关。我已经定义了一个方法来检查片段是否已加载,如果是,则不加载,但我认为无论如何都是重新加载。
10-13 12:48:34.809: E/AndroidRuntime(26460): FATAL EXCEPTION: main
10-13 12:48:34.809: E/AndroidRuntime(26460): Process: com.rowingsoft, PID: 26460
10-13 12:48:34.809: E/AndroidRuntime(26460): java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
10-13 12:48:34.809: E/AndroidRuntime(26460): at android.view.ViewGroup.addViewInner(ViewGroup.java:3759)
10-13 12:48:34.809: E/AndroidRuntime(26460): at android.view.ViewGroup.addView(ViewGroup.java:3612)
10-13 12:48:34.809: E/AndroidRuntime(26460): at android.view.ViewGroup.addView(ViewGroup.java:3557)
10-13 12:48:34.809: E/AndroidRuntime(26460): at android.view.ViewGroup.addView(ViewGroup.java:3533)
10-13 12:48:34.809: E/AndroidRuntime(26460): at android.support.v4.app.NoSaveStateFrameLayout.wrap(NoSaveStateFrameLayout.java:40)
10-13 12:48:34.809: E/AndroidRuntime(26460): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:931)
10-13 12:48:34.809: E/AndroidRuntime(26460): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
10-13 12:48:34.809: E/AndroidRuntime(26460): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1086)
10-13 12:48:34.809: E/AndroidRuntime(26460): at android.support.v4.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManager.java:1884)
10-13 12:48:34.809: E/AndroidRuntime(26460): at android.support.v4.app.Fragment.performActivityCreated(Fragment.java:1514)
10-13 12:48:34.809: E/AndroidRuntime(26460): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:947)
10-13 12:48:34.809: E/AndroidRuntime(26460): at android.support.v4.app.FragmentManagerImpl.attachFragment(FragmentManager.java:1280)
10-13 12:48:34.809: E/AndroidRuntime(26460): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:672)
10-13 12:48:34.809: E/AndroidRuntime(26460): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1467)
10-13 12:48:34.809: E/AndroidRuntime(26460): at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:472)
10-13 12:48:34.809: E/AndroidRuntime(26460): at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
10-13 12:48:34.809: E/AndroidRuntime(26460): at android.support.v4.view.ViewPager.populate(ViewPager.java:1068)
10-13 12:48:34.809: E/AndroidRuntime(26460): at android.support.v4.view.ViewPager.populate(ViewPager.java:914)
10-13 12:48:34.809: E/AndroidRuntime(26460): at android.support.v4.view.ViewPager$3.run(ViewPager.java:244)
10-13 12:48:34.809: E/AndroidRuntime(26460): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:803)
10-13 12:48:34.809: E/AndroidRuntime(26460): at android.view.Choreographer.doCallbacks(Choreographer.java:603)
10-13 12:48:34.809: E/AndroidRuntime(26460): at android.view.Choreographer.doFrame(Choreographer.java:572)
10-13 12:48:34.809: E/AndroidRuntime(26460): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:789)
10-13 12:48:34.809: E/AndroidRuntime(26460): at android.os.Handler.handleCallback(Handler.java:733)
10-13 12:48:34.809: E/AndroidRuntime(26460): at android.os.Handler.dispatchMessage(Handler.java:95)
10-13 12:48:34.809: E/AndroidRuntime(26460): at android.os.Looper.loop(Looper.java:157)
10-13 12:48:34.809: E/AndroidRuntime(26460): at android.app.ActivityThread.main(ActivityThread.java:5356)
10-13 12:48:34.809: E/AndroidRuntime(26460): at java.lang.reflect.Method.invokeNative(Native Method)
10-13 12:48:34.809: E/AndroidRuntime(26460): at java.lang.reflect.Method.invoke(Method.java:515)
10-13 12:48:34.809: E/AndroidRuntime(26460): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
10-13 12:48:34.809: E/AndroidRuntime(26460): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
10-13 12:48:34.809: E/AndroidRuntime(26460): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:2)
在花了一些时间试图解决这个问题,并寻找有关它的信息之后,我终于找到了一种方法让它发挥作用。
我意识到这不是最佳解决方案,因为这只能解决问题,但不能从根本解决问题。
无论如何,让我的应用程序工作,所以就在这里。
在定义viewPager时,我们设置页面限制,即我们拥有的页面数。在某种程度上,这可以避免内存泄漏并使其顺利运行。在一行中:
mViewPager.setOffscreenPageLimit(3);
此方法在此answer中提供。
答案 1 :(得分:1)
而不是transaction.add
使用transaction.replace
答案 2 :(得分:0)
你试过这个吗?
/*Check if fragment is already loaded*/
String tag = "myFrag";
Fragment frag = getChildFragmentManager().findFragmentByTag(tag);
if (frag == null) {
/*load fragment with 3d model*/
Fragment modelFragment = new Obj3DView();
FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
transaction.add(R.id.model3d_container, modelFragment, tag).commit();
}
如果你的XML中有一个片段,那个id就不是片段,而是它的一个FrameLayout。