gpt4 book ai didi

android - FragmentManager 已经在执行事务

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:56:39 29 4
gpt4 key购买 nike

所以我在将 ViewPager 与 FragmentStatePagerAdapter 一起使用时遇到了这个问题然后我试图选择当前页面(即向用户显示第二页而不是第一页)我不时得到这个异常.

问题似乎是并发性,因为 viewpager 仍在添加/初始化 fragment ,即当前正在运行 fragment 事务,而我正在尝试设置当前页面。像这样:

pagerAdapter = new MyFragmentPagerAdapter(getChildFragmentManager());
viewPager.setAdapter(pagerAdapter);
pagerAdapter.addItems(...) //Addings ID's here for each fragment, this is where the adapter notifies the viewpager
viewPager.setCurrentItem(1); //This is where the two transactions at the same time might happen

问题是:我怎么知道 viewpager 何时完成 fragment 初始化以便设置当前页面是安全的?

确切的异常(exception)是:

Fatal Exception: java.lang.IllegalStateException: FragmentManager is already executing transactions
at android.support.v4.app.FragmentManagerImpl.execSingleAction(FragmentManager.java:1631)
at android.support.v4.app.BackStackRecord.commitNowAllowingStateLoss(BackStackRecord.java:679)
at android.support.v4.app.FragmentStatePagerAdapter.finishUpdate(FragmentStatePagerAdapter.java:166)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1272)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1120)
at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1646)
at android.view.View.measure(View.java:18916)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5972)
at android.support.design.widget.CoordinatorLayout.onMeasureChild(CoordinatorLayout.java:703)
at android.support.design.widget.HeaderScrollingViewBehavior.onMeasureChild(HeaderScrollingViewBehavior.java:90)
at android.support.design.widget.AppBarLayout$ScrollingViewBehavior.onMeasureChild(AppBarLayout.java:1367)
at android.support.design.widget.CoordinatorLayout.onMeasure(CoordinatorLayout.java:768)
at android.view.View.measure(View.java:18916)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5972)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:139)
at android.view.View.measure(View.java:18916)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5972)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1465)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:748)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:630)
at android.view.View.measure(View.java:18916)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5972)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at android.view.View.measure(View.java:18916)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5972)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1465)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:748)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:630)
at android.view.View.measure(View.java:18916)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5972)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at com.android.internal.policy.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2693)
at android.view.View.measure(View.java:18916)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2231)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1307)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1549)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1198)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6268)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:873)
at android.view.Choreographer.doCallbacks(Choreographer.java:676)
at android.view.Choreographer.doFrame(Choreographer.java:606)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:859)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:168)
at android.app.ActivityThread.main(ActivityThread.java:5885)
at java.lang.reflect.Method.invoke(Method.java)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:797)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:687)

最佳答案

            Handler handler = new Handler();
handler.post(new Runnable() {
@Override
public void run() {
viewPager.setCurrentItem(1);
}
});

关于android - FragmentManager 已经在执行事务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41682257/

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