嵌套片段java.lang.IllegalStateException:活动已被销毁

时间:2015-01-05 06:34:07

标签: android android-fragments

我正在使用嵌套片段。我的代码如下:

 private void createGrid(List<App> appList) {

        if(appGridView!=null){
            getChildFragmentManager().beginTransaction().remove(appGridView).commitAllowingStateLoss();
            getChildFragmentManager().executePendingTransactions();
        }

        appGridView = new AppGridView();


        getChildFragmentManager().beginTransaction().add(R.id.frame_top_app_grid, appGridView).commitAllowingStateLoss();}

我还将其添加到我的代码中:

 @Override
    public void onDetach() {
        super.onDetach();
        // for bug fixing java.lang.IllegalStateException: Activity has been destroyed
        try {
            Field childFragmentManager = Fragment.class.getDeclaredField("mChildFragmentManager");
            childFragmentManager.setAccessible(true);
            childFragmentManager.set(this, null);

        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }

但是ACRA报告了我这次事故:

java.lang.IllegalStateException: Activity has been destroyed
at android.support.v4.app.FragmentManagerImpl.enqueueAction(SourceFile:1376)
at android.support.v4.app.BackStackRecord.commitInternal(SourceFile:595)
at android.support.v4.app.BackStackRecord.commitAllowingStateLoss(SourceFile:578)

请帮我解决这个问题。

1 个答案:

答案 0 :(得分:0)

我将createGrid放入:

if(isadded){
creategrid()
}else{
recreate activity
}
问题解决了。