提问者:小点点

碎片当前不在碎片管理器中


我搜索了其他人类似的帖子,但我没有找到解决我的问题的方法。

我正在使用一个滑动菜单库(我知道现在有一个官方解决方案,但不管怎样)所以我得到了这个FragmentChange活动:

    @Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // set the Above View
    if (savedInstanceState != null)
        mContent = getSupportFragmentManager().getFragment(savedInstanceState, "mContent");
    if (mContent == null)
        mContent = new AccueilFragment();   

    // set the Above View
    setContentView(R.layout.content_frame);
    getSupportFragmentManager()
    .beginTransaction()
    .replace(R.id.content_frame, mContent)
    .commit();

    // set the Behind View
    setBehindContentView(R.layout.menu_frame);
    getSupportFragmentManager()
    .beginTransaction()
    .replace(R.id.menu_frame, new AppliMenuFragment())
    .commit();

    // customize the SlidingMenu
    getSlidingMenu().setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);

}

@Override
public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    getSupportFragmentManager().putFragment(outState, "mContent", mContent);
}

public void switchContent(Fragment fragment) {
    mContent = fragment;
    String tag = null;
    getSupportFragmentManager()
    .beginTransaction()
    .replace(R.id.content_frame, fragment)
    .addToBackStack(tag)
    .commit();
    getSlidingMenu().showContent();
}

从那里,它将在菜单中单击项目时将每个片段替换为另一个片段。它工作正常。例如,问题是:

  • 片段A至-

这里我有一个例外:

Java . lang . illegalstateexception:Fragment Fragment a当前不在FragmentManager中

我发现问题是下面的代码在哪里执行:

getSupportFragmentManager().putFragment(outState, "mContent", mContent);

但我不明白为什么。任何帮助都是徒劳的!

-----编辑-----

只有当我按下返回按钮,然后按下主页按钮时,才会出现这种情况。如果我只按home键,getSupportFragmentManager()。放一个片段,工作正常。

我知道我不能做任何片段事务后,SavedInstanceState()被调用,但getBackportFragmentManager(). utFra的()不是一个事务对吗?或者我错过了什么?我不完全舒服。

-----溶液-----

我仍然不明白为什么getSofportFragmentManager(). utFra的()它是不可能的,但从那里,我删除了它,并使用findFragmentByTag在on创建:如果(保存InstanceState!=null)mContent=getBackportFragmentManager(). findFragmentByTag("定制标签");如果(mContent==null)mContent=new AccueilFra的();
它有完全相同的行为,保持状态时旋转和没有更多的异常,我按返回然后回家。


共1个答案

匿名用户

我从这里得到了这个:getSupportFragmentManager().putFragment的问题

在onSaveInstanceState之后,您不能进行片段事务