在多Fragment 切换时报了这个异常, google 搜了一些答案都没能解决这个问题.
app 布局是这样的:刚开始是一个Fragment,里面有 viewpager ,viewpager 里面有三个 fragment, 然后是侧滑导航栏可切换到其他的 Fragment 但切换回来第一个界面的时候报了这个错误,不知道是布局的问题还是什么问题...是因为用了 v4 包的 Fragment 吗?看别人没用 v4包的 Fragment 不会报错,求解~
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.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:143)
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.setOffscreenPageLimit(ViewPager.java:884)
at com.muxistudio.jobs.ui.find.FindFragment.setupViewPager(FindFragment.java:69)
at com.muxistudio.jobs.ui.find.FindFragment.initView(FindFragment.java:43)
at com.muxistudio.jobs.ui.BaseFragment.onViewCreated(BaseFragment.java:42)
at com.muxistudio.jobs.ui.find.FindFragment.onViewCreated(FindFragment.java:75)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1132)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1295)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:801)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1682)
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:541)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
第一个界面的 Fragment 中添加子 Fragment(在第二次切换到这个界面执行 mViewPager.setOffscreenPageLimit(2); 时抛出了这个异常)
for (int i = 0; i < 3; i++) {
mTabLayout.addTab(mTabLayout.newTab().setText(titleList.get(i)));
}
mTabLayout.setBackgroundColor(Color.WHITE);
mTabLayout.setTabTextColors(Color.BLACK, getResources().getColor(R.color.colorAccent));
List<Fragment> fragments = new ArrayList<>();
fragments.add(InfoFragment.newInstance(1));
fragments.add(InfoFragment.newInstance(2));
fragments.add(InfoFragment.newInstance(3));
FragmentAdapter adapter =
new FragmentAdapter(getActivity().getSupportFragmentManager(), fragments, titleList);
mViewPager.setAdapter(adapter);
mViewPager.setOffscreenPageLimit(2);
mTabLayout.setupWithViewPager(mViewPager);
切换 fragment
@Override public void showFragment(Fragment fragment) {
getSupportFragmentManager().beginTransaction().replace(R.id.content, fragment).commit();
Logger.d((fragment instanceof FindFragment) + "");
if (fragment instanceof FindFragment) {
invalidateOptionsMenu();
}
}
OnNavigationItemClick(mMainView是MainActivity实现的一个接口对象,用了 MVP,来回切换就会报这个异常...)
@Override public void onNavigationItemClick(MenuItem item) {
switch (item.getItemId()){
case R.id.action_find:
mMainView.showFragment(FindFragment.newInstance());
break;
case R.id.action_date:
break;
case R.id.action_person:
mMainView.showFragment(CollectionFragment.newInstance());
break;
case R.id.action_discuss:
break;
case R.id.action_setting:
mMainView.showSetting();
break;
case R.id.action_about:
break;
}
mMainView.setTitle(item.getTitle().toString());
}
Selesai. Saya mengalami masalah ini selama beberapa hari dan kemudian apabila saya mencari Stack OverFlow, saya menjumpai jawapan yang menyelesaikan masalah saya
FragmentManager sudah melaksanakan transaksi Bilakah selamat untuk memulakan kelui selepas komit?
hendaklah ditukar kepada
Adakah anda telah menyelesaikan masalah yang saya hadapi masalah yang sama seperti anda.