所以当我将ViewPager与FragmentStatePagerAdapter一起使用时遇到这个问题然后我试图选择当前页面(即向用户显示第二页而不是第一页),我不时会得到这个例外
问题似乎是并发性,因为viewpager仍在添加/初始化片段,即当我正在尝试设置当前页面时正在运行片段事务。像这样:
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何时完成片段的初始化,以便设置当前页面是安全的?
确切的例外是:
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)
答案 0 :(得分:8)
Handler handler = new Handler();
handler.post(new Runnable() {
@Override
public void run() {
viewPager.setCurrentItem(1);
}
});
答案 1 :(得分:0)
对于使用 Kotlin 并遇到此问题的人 Handler() 构造函数已弃用,您可以使用它:
Handler(Looper.getMainLooper()).post {
// interact with ViewPager here
}