我正在尝试保存两个片段的状态。当我改变Android设备的语言时(当我离开活动并返回时),我希望它们重新显示。这两个片段被声明为活动的属性,并且都扩展了 android.support.v4.app.Fragment 类。这就是我在显示它们的活动中保存它们的方法:
@Override
protected void onSaveInstanceState(Bundle outState) {
if(settingsFragment.isAdded())
this.getSupportFragmentManager().putFragment(outState,settingsFragment.TAG,settingsFragment);
if(historyFragment.isAdded())
this.getSupportFragmentManager().putFragment(outState,historyFragment.TAG,historyFragment);
}
这是检索它们的方法(请记住,这是在onCreate()方法中调用的方法,就在setContentView()方法之后):
private void instantiateFragments(Bundle inState) {
FragmentManager manager = getFragmentManager();
if (inState != null) { //the line 254 is just below
settingsFragment = (SettingsFragment) this.getSupportFragmentManager().getFragment(inState, SettingsFragment.TAG);
historyFragment = (HistoryFragment) this.getSupportFragmentManager().getFragment(inState, HistoryFragment.TAG);
} else {
settingsFragment = SettingsFragment.newInstance("Settings","0");
historyFragment = HistoryFragment.newInstance("History","1");
}
}
这是我在尝试加载活动时收到的错误:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example/com.example.UI.Activities.ExampleActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'int java.util.ArrayList.size()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2802)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2867)
at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:4594)
at android.app.ActivityThread.access$1000(ActivityThread.java:181)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1482)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:6134)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int java.util.ArrayList.size()' on a null object reference
at android.support.v4.app.FragmentManagerImpl.getFragment(FragmentManager.java:872)
at com.example.UI.Activities.ExampleActivity.instantiateFragments(ExampleActivity.java:254)
at com.example.UI.Activities.ExampleActivity.onCreate(ExampleActivity.java:102)
at android.app.Activity.performCreate(Activity.java:6374)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2746)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2867)
at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:4594)
at android.app.ActivityThread.access$1000(ActivityThread.java:181)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1482)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:6134)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
你们能帮助我了解发生了什么,我该如何解决这个问题(意思是,如何从它的活动中保存片段的实例)。谢谢
修改 对于那些要求我显示和隐藏的人来说,要显示settingsFragment,这就是我使用的方法:
protected void displayFragmentSettings() {
FragmentTransaction ft = this.getSupportFragmentManager().beginTransaction();
if (settingsFragment.isAdded()) { // if the fragment is already in container
ft.show(settingsFragment);
} else { // fragment needs to be added to frame container
ft.add(R.id.body, settingsFragment, "Settings");
}
// Hide fragment B
if (historyFragment.isAdded()) { ft.hide(historyFragment); }
// Hide fragment C
// if (fragmentC.isAdded()) { ft.hide(fragmentC); }
// Commit changes
ft.commit();
}
我对我需要显示的任何其他片段也这样做。
答案 0 :(得分:2)
我认为不是保存片段的实例,为什么不隐藏第一个片段并添加第二个片段而不是替换它。如果要显示第一个片段,请在第一个片段上调用show并隐藏在第二个片段上。
getSupportFragmentManager()的BeginTransaction()显示(片段1).commit()。; 。getSupportFragmentManager()的BeginTransaction()隐藏(fragment2).commit();
答案 1 :(得分:1)
主要问题是我忘记添加super.onSaveInstanceState(...)方法:
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
//this.getSupportFragmentManager().putFragment(outState, myFragment.TAG, myFragment);
}