具有旧Activity引用的FragmentManager(java.lang.IllegalStateException)

时间:2014-12-01 08:57:22

标签: android android-fragments android-fragmentactivity fragmentmanager

我正在使用v4-lib开发一个应用程序。在FragmentActivity中,我有一个FragmentPagerAdapter来保存我的碎片。滑过我的所有片段后,我在Activity上调用Finish()。现在它变得复杂了:当重新启动应用程序时(未通过TaskManager完全杀死),屏幕保持白色,系统显示:

  

12-01 09:49:44.933 10161-10161 / com.xxx.yyy W / System.err:java.lang.IllegalStateException:活动已被销毁   12-01 09:49:44.933 10161-10161 / com.xxx.yyy W / System.err:at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1387)   12-01 09:49:44.933 10161-10161 / com.xxx.yyy W / System.err:at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:636)   12-01 09:49:44.933 10161-10161 / com.xxx.yyy W / System.err:at android.support.v4.app.BackStackRecord.commitAllowingStateLoss(BackStackRecord.java:619)   12-01 09:49:44.933 10161-10161 / com.xxx.yyy W / System.err:at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:139)   12-01 09:49:44.933 10161-10161 / com.xxx.yyy W / System.err:在android.support.v4.view.ViewPager.setAdapter(ViewPager.java:420)   12-01 09:49:44.933 10161-10161 / com.xxx.yyy W / System.err:at com.yyy.zzz_lib.ui.AbstractQuestionnaireActivity.setUpLayout(AbstractQuestionnaireActivity.java:78)   12-01 09:49:44.933 10161-10161 / com.xxx.yyy W / System.err:at com.yyy.zzz.QuestionnaireActivity.setContent(QuestionnaireActivity.java:58)   12-01 09:49:44.933 10161-10161 / com.xxx.yyy W / System.err:at com.yyy.zzz.QuestionnaireActivity.handleMessage(QuestionnaireActivity.java:100)   12-01 09:49:44.933 10161-10161 / com.xxx.yyy W / System.err:在android.os.Handler.dispatchMessage(Handler.java:98)   12-01 09:49:44.933 10161-10161 / com.xxx.yyy W / System.err:在android.os.Looper.loop(Looper.java:136)   12-01 09:49:44.933 10161-10161 / com.xxx.yyy W / System.err:在android.app.ActivityThread.main(ActivityThread.java:5586)   12-01 09:49:44.933 10161-10161 / com.xxx.yyy W / System.err:at java.lang.reflect.Method.invokeNative(Native Method)   12-01 09:49:44.933 10161-10161 / com.xxx.yyy W / System.err:at java.lang.reflect.Method.invoke(Method.java:515)   12-01 09:49:44.933 10161-10161 / com.xxx.yyy W / System.err:at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1268)   12-01 09:49:44.933 10161-10161 / com.xxx.yyy W / System.err:at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)   12-01 09:49:44.933 10161-10161 / com.xxx.yyy W / System.err:at dalvik.system.NativeStart.main(Native Method)

稍微调试一下后,我注意到当我重新打开活动时,仍有旧的FM上一个Activity的引用,稍后在ViewPager上设置适配器时,Activity为null并且应用程序崩溃。通过TaskManager清理整个应用程序时,所有操作都会以正确的方式进行一次 我尝试了几件事:弹出整个后台堆栈,保存对FragmentManager的新引用,玩Fragment.retainInstanceState()等等......遗憾的是没有任何结果。我认为不可能获得FragmentManager的干净实例。有人可以帮我解决这个问题吗?

更新:一些代码段:

public abstract class AbstractQuestionnaireActivity extends FragmentActivity {

  private QuestionnaireModel mQuestionnaireModel;

  private QuestionnaireViewPager mPager;
  private QuestionnairePagerAdapter mPagerAdapter;

  public void setUpLayout(int layoutRoot) {

    try {
      FrameLayout root = (FrameLayout) findViewById(layoutRoot);

      //inflate questionnaire layout
      View questionnaire = LayoutInflater.from(this).inflate(R.layout.elem_questionnaire, null);
      root.addView(questionnaire);

      mPagerAdapter = new QuestionnairePagerAdapter(getSupportFragmentManager());
      mPager = (QuestionnaireViewPager) findViewById(R.id.pager);
      mPager.setPagingEnabled(false);
      mPager.setAdapter(mPagerAdapter);

      ...

    } catch (ClassCastException e) {
      throw new ClassCastException("Layout root must be a FrameLayout!");
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

  ...

  public class QuestionnairePagerAdapter extends FragmentPagerAdapter {

    public QuestionnairePagerAdapter(FragmentManager fm) {
      super(fm);
    }

    @Override
    public Fragment getItem(int pos) {
      return mQuestionnaireModel.getQuestions().get(pos).createFragment(pos);
    }

    @Override
    public int getCount() {
      return mQuestionnaireModel.getQuestions().size();
    }
  }
}

0 个答案:

没有答案