我正在使用支持库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)
我也尝试调用返回null的getFragments().这在我看来很奇怪.
我将不胜感激任何帮助!
解:
我忘了使用super.onSaveInstanceState(outState);在onCreate方法中.
最佳答案
感谢@Fllo.问题是我忘了调用父活动的onSaveInstanceState().
@Override
protected void onSaveInstanceState(Bundle outState)
{
//Added this and the problem was solved
super.onSaveInstanceState(outState);
...
}