使用方法setOffscreenPageLimit()后,FragmentManager已经在执行事务

Mer*_*280 13 android android-fragments android-viewpager android-tablayout

我有一个带有多个片段的导航抽屉,其中一个是带有两个recyclerview片段的tablayout,在导航抽屉片段之间切换后,RecyclerView项目消失了,我找到了一个解决方案,我需要使用方法setOffscreenPageLimit()和ViewPager但它给了我问题标题中提到的错误

这是我的logcat

java.lang.IllegalStateException: FragmentManager is already executing transactions
                                                                               at android.support.v4.app.FragmentManagerImpl.execSingleAction(FragmentManager.java:1626)
                                                                               at android.support.v4.app.BackStackRecord.commitNowAllowingStateLoss(BackStackRecord.java:679)
                                                                               at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:143)
                                                                               at android.support.v4.view.ViewPager.populate(ViewPager.java:1240)
                                                                               at android.support.v4.view.ViewPager.populate(ViewPager.java:1088)
                                                                               at android.support.v4.view.ViewPager.setOffscreenPageLimit(ViewPager.java:852)
                                                                               at com.amir.ahmed.EELUStudentUnion.SelectionFragment.setupViewPager(SelectionFragment.java:59)
                                                                               at com.amir.ahmed.EELUStudentUnion.SelectionFragment.onActivityCreated(SelectionFragment.java:40)
                                                                               at android.support.v4.app.Fragment.performActivityCreated(Fragment.java:2089)
                                                                               at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1133)
                                                                               at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1290)
                                                                               at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:801)
                                                                               at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1677)
                                                                               at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:536)
                                                                               at android.os.Handler.handleCallback(Handler.java:739)
                                                                               at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                               at android.os.Looper.loop(Looper.java:158)
                                                                               at android.app.ActivityThread.main(ActivityThread.java:7225)
                                                                               at java.lang.reflect.Method.invoke(Native Method)
                                                                               at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
                                                                               at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
Run Code Online (Sandbox Code Playgroud)

这是标签布局片段代码

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    viewPager = (ViewPager) getActivity().findViewById(R.id.viewpager);
    setupViewPager(viewPager);

    viewPager.setOffscreenPageLimit(2);

    tabLayout = (TabLayout) getActivity().findViewById(R.id.tabs);
    tabLayout.setupWithViewPager(viewPager);
    setupTabIcons();
}



private void setupViewPager(ViewPager viewPager) {
    ViewPagerAdapter adapter = new ViewPagerAdapter(getFragmentManager());
    adapter.addFrag(new Courses(), "IT");
    adapter.addFrag(new CoursesBussiness(), "Business");
    viewPager.setAdapter(adapter);
}
Run Code Online (Sandbox Code Playgroud)

Wei*_*ang 35

我遇到了与你类似的问题,经过几个小时的研究,我发现了这个问题.如果在Fragment中创建ViewPager,则需要对FragmentPagerAdapter使用getChildFragmentManager(),而不是getFragmentManager().

所以你的setupViewPager()方法应该是这样的.

private void setupViewPager(ViewPager viewPager) {
    ViewPagerAdapter adapter = new ViewPagerAdapter(getChildFragmentManager());
    adapter.addFrag(new Courses(), "IT");
    adapter.addFrag(new CoursesBussiness(), "Business");
    viewPager.setAdapter(adapter);
}
Run Code Online (Sandbox Code Playgroud)

问题的根本原因是你在Fragment中嵌入了Fragments,这就是它抛出异常的原因.

java.lang.IllegalStateException: FragmentManager is already executing transactions
Run Code Online (Sandbox Code Playgroud)

根据谷歌的文档:

您现在可以将片段嵌入片段中.这对于您希望将动态和可重用UI组件放入自身动态且可重用的UI组件的各种情况都很有用.例如,如果使用ViewPager创建向左和向右滑动并占用大部分屏幕空间的片段,则现在可以将片段插入到每个片段页面中.要嵌套片段,只需在要添加片段的片段上调用getChildFragmentManager().这将返回一个FragmentManager,您可以像通常从顶级活动那样使用它来创建片段事务.

  • 我从2016年9月开始就遇到了这个问题,你的回答帮助我解决了这个问题.万分感谢!!!! (2认同)
  • 不适合我。当我添加TabLayout时,在onAttachFragment方法中选择该错误。 (2认同)