cari

Rumah  >  Soal Jawab  >  teks badan

android - FragmentManager is already executing transactions异常

在多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)

代码

 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);
  @Override public void showFragment(Fragment fragment) {
    getSupportFragmentManager().beginTransaction().replace(R.id.content, fragment).commit();
    Logger.d((fragment instanceof FindFragment) + "");
    if (fragment instanceof FindFragment) {
      invalidateOptionsMenu();
    }
  }
  @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中文网2771 hari yang lalu2461

membalas semua(2)saya akan balas

  • PHP中文网

    PHP中文网2017-04-17 18:03:03

    解决了,我这个问题放了几天后来去搜Stack OverFlow时发现有个回答解决了我的问题
    FragmentManager is already executing transactions. When is it safe to initialise pager after commit?

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

    要改为

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

    balas
    0
  • PHP中文网

    PHP中文网2017-04-17 18:03:03

    楼主解决问题了没有,我和你遇到了一样的问题。

    balas
    0
  • Batalbalas