在onPauseState中崩溃

时间:2014-02-04 15:54:39

标签: android android-activity illegalstateexception

暂停时我的活动崩溃了。我可以从日志中看到,它在saveInstance中失败了。我不知道如何解决它。

protected void onSaveInstanceState(Bundle savedInstanceState) 
    {           
        NavFragment navListFragment = (NavFragment)getSupportFragmentManager().findFragmentByTag(Const.TAG_FRAGMENT_NAV);   
        if (navListFragment != null)
        {
            savedInstanceState.putInt(Const.STATE_VAR_NAV_INDEX, navListFragment.getSelection());
            Log.i("STATE", "navListFragment.getSelection(): " + navListFragment.getSelection());
        }

        Fragment contentFragment = getSupportFragmentManager().findFragmentByTag(Const.TAG_FRAGMENT_CONTENT);
        if(contentFragment != null && contentFragment instanceof BasicContentFragment)
        {                       
            savedInstanceState.putBoolean(Const.STATE_VAR_IS_CONTENT, true);
            savedInstanceState.putInt(Const.STATE_VAR_CONTENT_INDEX, ((BasicContentFragment)contentFragment).getCurrentPage());

            if(Const.IS_LARGE_SCREEN)
            {
                SlidingDrawer drawer = (SlidingDrawer)findViewById(R.id.selector_drawer);
                if(drawer != null)              
                    savedInstanceState.putBoolean(Const.STATE_VAR_IS_LIST_VISIBLE, drawer.isOpened());                          
            }
        }

        savedInstanceState.putString(Const.STATE_VAR_HEADING, mHeading);

        super.onSaveInstanceState(savedInstanceState); // crashes in this line
    }

日志:

02-04 10:50:20.320: E/AndroidRuntime(23650): FATAL EXCEPTION: main
02-04 10:50:20.320: E/AndroidRuntime(23650): java.lang.IllegalStateException: Failure saving state: active DialogFactory{407dd070} has cleared index: -1
02-04 10:50:20.320: E/AndroidRuntime(23650): at android.support.v4.app.FragmentManagerImpl.saveAllState(FragmentManager.java:1695)
02-04 10:50:20.320: E/AndroidRuntime(23650): at android.support.v4.app.FragmentActivity.onSaveInstanceState(FragmentActivity.java:499)
02-04 10:50:20.320: E/AndroidRuntime(23650): at com.philly.prosportsframework.activity.MainFragmentActivity.onSaveInstanceState(MainFragmentActivity.java:625)

1 个答案:

答案 0 :(得分:0)

DialogFactory中的Fragment FragmentManager之后看起来确实有一个“剩余”,但Fragment本身已经清除了它的状态。因此,当FragmentManager尝试保存状态时,它会失败。

尝试通过FragmentTransaction.remove(Fragment fragment)方法从DialogFactory明确删除FragmentManager。如果在保存状态之前(以及在提交删除事务之后)尝试调用FragmentManager.executePendingTransaction()没有帮助。