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

nested fragment java.lang.IllegalStateException: Activity has been destroyed

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

 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)

请帮我解决这个问题。

我将 createGrid 放入:

if(isadded){
creategrid()
}else{
recreate activity
}

问题解决了。