如何使用onSaveInstanceState()和onRestoreInstanceState()保存和恢复多维数组?
如何使用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}: 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.performLaunchActivity(ActivityThread.java:2663) 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.moveToState(LocalActivityManager.java:127) 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):在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 更多
有没有办法存储和检索多维数组?
How to use onSaveInstanceState() and onRestoreInstanceState() to save and restore multi-dimensional array?
I am using ExpandableListView. I have following groups and children.
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);
}
I got following java.lang.ClassCastException exception.
01-19 17:12:55.417: E/AndroidRuntime(708): Caused by: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.abc.myapp/ com.abc.myapp.mylistActivity}: java.lang.ClassCastException: [Ljava.lang.Object;
01-19 17:12:55.417: E/AndroidRuntime(708): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
01-19 17:12:55.417: E/AndroidRuntime(708): at android.app.ActivityThread.startActivityNow(ActivityThread.java:2503)
01-19 17:12:55.417: E/AndroidRuntime(708): at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:127)
01-19 17:12:55.417: E/AndroidRuntime(708): at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:339)
01-19 17:12:55.417: E/AndroidRuntime(708): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
01-19 17:12:55.417: E/AndroidRuntime(708): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
01-19 17:12:55.417: E/AndroidRuntime(708): ... 11 more
01-19 17:12:55.417: E/AndroidRuntime(708): Caused by: 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): at android.app.Activity.performRestoreInstanceState(Activity.java:815)
01-19 17:12:55.417: E/AndroidRuntime(708): at android.app.Instrumentation.callActivityOnRestoreInstanceState(Instrumentation.java:1096)
01-19 17:12:55.417: E/AndroidRuntime(708): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2641)
01-19 17:12:55.417: E/AndroidRuntime(708): ... 19 more
Is there any way to store and retrieve multi-dimensional array?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论