android - FragmentManager is already executing transactions异常
PHP中文网
PHP中文网 2017-04-17 18:01:03
0
2
2491

在多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());
  }
PHP中文网
PHP中文网

认证0级讲师

membalas semua(2)
Ty80

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?

FragmentAdapter adapter =
        new FragmentAdapter(getActivity().getSupportFragmentManager(), fragments, titleList);

hendaklah ditukar kepada

FragmentAdapter adapter = new FragmentAdapter(getChildFragmentManager(),fragments,titleList);
洪涛

Adakah anda telah menyelesaikan masalah yang saya hadapi masalah yang sama seperti anda.

Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan