gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:51:27 29 4
gpt4 key购买 nike

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

这是我的日志

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)

标签布局 fragment 代码

@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);
}

最佳答案

我遇到了和你类似的问题,经过几个小时的研究,我发现了这个问题。如果您的 ViewPager 是在 Fragment 内部创建的,则需要为 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);
}

问题的根本原因是你在 fragment 中嵌入了 fragment ,这就是它会抛出异常的原因。

java.lang.IllegalStateException: FragmentManager is already executing transactions

根据 Google 的文档:

You can now embed fragments inside fragments. This is useful for a variety of situations in which you want to place dynamic and re-usable UI components into a UI component that is itself dynamic and re-usable. For example, if you use ViewPager to create fragments that swipe left and right and consume a majority of the screen space, you can now insert fragments into each fragment page. To nest a fragment, simply call getChildFragmentManager() on the Fragment in which you want to add a fragment. This returns a FragmentManager that you can use like you normally do from the top-level activity to create fragment transactions.

关于android - FragmentManager 在使用方法 setOffscreenPageLimit() 后已经在执行事务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40094023/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com