如何使用onSaveInstanceState()和onRestoreInstanceState()来保存和恢复多维数组?
我正在使用ExpandableListView。我有以下团体和孩子。
public static String[] groups = new String[] { "New York", "California",
"Washington", "Florida", "Iowa"};
public static String[][] children = new String[][] { { } };
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
.........................
if(condition){
children = populateChildren();
}
.........................
}
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
if (groups != null && children != null) {
savedInstanceState.putStringArray("Groups", groups);
savedInstanceState.putSerializable("Children", children);
}
super.onSaveInstanceState(savedInstanceState);
}
@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
if (savedInstanceState != null) {
groups = savedInstanceState.getStringArray("Groups");
children = (String[][]) savedInstanceState
.getSerializable("Children");
}
super.onRestoreInstanceState(savedInstanceState);
}
我收到了java.lang.ClassCastException异常。
01-19 17:12:55.417:E / AndroidRuntime(708):引起:java.lang.RuntimeException:无法启动活动ComponentInfo {com.abc.myapp / com.abc.myapp.mylistActivity}:< strong> java.lang.ClassCastException:[Ljava.lang.Object;
01-19 17:12:55.417:E / AndroidRuntime(708):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
01-19 17:12:55.417:E / AndroidRuntime(708):在android.app.ActivityThread.startActivityNow(ActivityThread.java:2503)
01-19 17:12:55.417:E / AndroidRuntime(708):在android.app.LocalActivityManager.moveToState(LocalActivityManager.java:127)
01-19 17:12:55.417:E / AndroidRuntime(708):在android.app.LocalActivityManager.startActivity(LocalActivityManager.java:339)
01-19 17:12:55.417:E / AndroidRuntime(708):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
01-19 17:12:55.417:E / AndroidRuntime(708):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
01-19 17:12:55.417:E / AndroidRuntime(708):... 11更多 01-19 17:12:55.417:E / AndroidRuntime(708):引起: java.lang.ClassCastException:[Ljava.lang.Object; 01-19 17:12:55.417:E / AndroidRuntime(708):at com.abc.myapp.mylistActivity.onRestoreInstanceState(JobInfo.java:303)
01-19 17:12:55.417:E / AndroidRuntime(708):在android.app.Activity.performRestoreInstanceState(Activity.java:815)
01-19 17:12:55.417:E / AndroidRuntime(708):在android.app.Instrumentation.callActivityOnRestoreInstanceState(Instrumentation.java:1096)
01-19 17:12:55.417:E / AndroidRuntime(708):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2641)
01-19 17:12:55.417:E / AndroidRuntime(708):... 19更多
有没有办法存储和检索多维数组?