我一直在导航抽屉的某些片段中实现导航抽屉和标签很长一段时间。问题永远不会出现,它仍然没有出现在少数片段中,但它只出现在导航抽屉中的两个片段中。
在导航栏中,每个项目代表一个由以下代码打开的片段
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
currentFragment = new SomeFragment();
home_container_drawer_layout.closeDrawers();
fragmentTransaction = setFTAnimation(fragmentTransaction);
fragmentTransaction.replace(R.id.framelayout_activity_home, currentFragment).commitNow();
一旦加载了片段,我就使用我创建的ION包装器进行服务调用,并在响应时在该片段的ViewPager中动态设置选项卡
以下是设置标签监听器和设置viewpager adapter
的两个函数 private void addListeners() {
pager_account_cash_mail.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tablayout_fragment_account_cash_mail));
tablayout_fragment_account_cash_mail.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
pager_account_cash_mail.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
}
private void setAdapter() {
mAdapter = new CashMailPagerAdapter(this.getChildFragmentManager(), tabs_fragments_list);
pager_account_cash_mail.setAdapter(mAdapter);
pager_account_cash_mail.setOffscreenPageLimit(tabs_fragments_list.size());
}
此外,我的代码包含addTab函数,该函数在服务响应后动态地将选项卡添加到选项卡布局中,并且在添加选项卡之后我调用这两个函数。
每当setAdapter行执行时,调试器进入viewpager适配器的getItem函数,一旦所有项都返回,应用程序崩溃说:
进程:com.foo.myapplication,PID:17278 java.lang.IllegalStateException:FragmentManager已经在执行事务 在android.support.v4.app.FragmentManagerImpl.execSingleAction(FragmentManager.java:1626) 在android.support.v4.app.BackStackRecord.commitNowAllowingStateLoss(BackStackRecord.java:679) 在android.support.v4.app.FragmentStatePagerAdapter.finishUpdate(FragmentStatePagerAdapter.java:166) 在android.support.v4.view.ViewPager.populate(ViewPager.java:1240) 在android.support.v4.view.ViewPager.populate(ViewPager.java:1088) 在android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1614) 在android.view.View.measure(View.java:17356) 在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5367) 在android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1467) 在android.widget.LinearLayout.measureVertical(LinearLayout.java:747) 在android.widget.LinearLayout.onMeasure(LinearLayout.java:638) 在android.view.View.measure(View.java:17356) 在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5367) 在android.widget.FrameLayout.onMeasure(FrameLayout.java:430) 在android.view.View.measure(View.java:17356) 在android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:727) 在android.widget.RelativeLayout.onMeasure(RelativeLayout.java:463) 在android.view.View.measure(View.java:17356) 在android.support.v4.widget.DrawerLayout.onMeasure(DrawerLayout.java:1085) 在android.view.View.measure(View.java:17356) 在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5367) 在android.widget.FrameLayout.onMeasure(FrameLayout.java:430) 在android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:135) 在android.view.View.measure(View.java:17356) 在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5367) 在android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1467) 在android.widget.LinearLayout.measureVertical(LinearLayout.java:747) 在android.widget.LinearLayout.onMeasure(LinearLayout.java:638) 在android.view.View.measure(View.java:17356) 在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5367) 在android.widget.FrameLayout.onMeasure(FrameLayout.java:430) 在android.view.View.measure(View.java:17356) 在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5367) 在android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1467) 在android.widget.LinearLayout.measureVertical(LinearLayout.java:747) 在android.widget.LinearLayout.onMeasure(LinearLayout.java:638) 在android.view.View.measure(View.java:17356) 在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5367) 在android.widget.FrameLayout.onMeasure(FrameLayout.java:430) 在com.android.internal.policy.impl.PhoneWindow $ DecorView.onMeasure(PhoneWindow.java:2576) 在android.view.View.measure(View.java:17356) 在android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2056) 在android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1199) 在android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1405) 在android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1087) 在android.view.ViewRootImpl $ TraversalRunnable.run(ViewRootImpl.java:6040) 在android.view.Choreographer $ CallbackRecord.run(Choreographer.java:793) 在android.view.Choreographer.doCallbacks(Choreographer.java:606) 在android.view.Choreographer.doFrame(Choreographer.java:575) 在android.view.Choreographer $ FrameDisplayEventReceiver.run(Choreographer.java:779) 在android.os.Handler.handleCallback(Handler.java:739) 在android.os.Handler.dispatchMessage(Handler.java:95) 在android.os.Looper.loop(Looper.java:135) 在android.app.ActivityThread.main(ActivityThread.java:5538) at java.lang.reflect.Method.invoke(Native Method) 在java.lang.reflect.Method.invoke(Method.java:372) 在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:960) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)