如何使用onSaveInstanceState()和onRestoreInstanceState()保存和恢复多维数组?

发布于 2024-12-28 02:43:02 字数 3141 浏览 0 评论 0原文

如何使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文