我正在使用支持库v4,未能保存片段的状态。
@Override
protected void onSaveInstanceState(Bundle outState)
{
...
if(mFragment != null)
getSupportFragmentManager().putFragment(outState, "frag", mFragment);
...
}
@Override
protected void onCreate(Bundle savedInstanceState)
{
...
if(savedInstanceState != null)
{
//Here I get NullPointerException
getSupportFragmentManager().getFragment(savedInstanceState, "frag");
}
...
}
当我调用getfragment()时,抛出nullpointerexception(savedinstancestate包含带有“frag”的条目)。
以下是logcat输出的一部分:
04-18 00:10:56.719: E/AndroidRuntime(14644): Caused by: java.lang.NullPointerException
04-18 00:10:56.719: E/AndroidRuntime(14644): at android.support.v4.app.FragmentManagerImpl.getFragment(FragmentManager.java:569)
我也试过调用
getFragments()
来返回null
。我觉得很奇怪。我将感谢您的帮助!
解决方案:
我忘了在内部使用
super.onSaveInstanceState(outState);
方法。 最佳答案
感谢@fllo。问题是我忘了给家长活动的onSaveInstanceState()
打电话。
@Override
protected void onSaveInstanceState(Bundle outState)
{
//Added this and the problem was solved
super.onSaveInstanceState(outState);
...
}