1

我正在尝试保存两个片段的状态。我希望它们在我更改 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();
}

我对需要显示的任何其他 Fragment 执行相同的操作。

4

2 回答 2

2

我认为与其保存片段的实例,不如隐藏第一个片段并添加第二个片段而不是替换它。当您想显示第一个片段时,请在第一个片段上调用 ​​show 并在第二个片段上隐藏。

getSupportFragmentManager().beginTransaction().show(fragment1).commit(); getSupportFragmentManager().beginTransaction().hide(fragment2).commit();

于 2017-01-18T22:32:55.613 回答
1

主要问题是我忘记添加 super.onSaveInstanceState(...) 方法:

 @Override
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    //this.getSupportFragmentManager().putFragment(outState, myFragment.TAG, myFragment);
}
于 2017-03-10T14:25:32.907 回答