我在让我的应用程序由于切换而暂停时保持其状态时遇到问题。我重载了 onSaveInstanceState 和 onRestoreInstanceState 方法来保存和恢复包含对我的应用程序状态很重要的变量的唯一对象(由一堆数组、数组列表、哈希集、枚举和原语组成)。不幸的是,从我的应用程序切换会导致它因序列化错误而崩溃。这是我用于保存和恢复状态的代码:
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
// Next line causes error when switching apps, but not when rotating screen
savedInstanceState.putSerializable("board", board);
}
@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
board = (Board)savedInstanceState.getSerializable("board");
}
据我所知,我唯一的问题是我创建的 Board 对象不可序列化。但是,它实现了 Serializable 并定义了它的 serialVersionUID,所以我不知道我还需要做什么才能使其可序列化。通常这就是所有必要的。
我有两个问题:
- 有没有比 getSerializable 方法更好的方法来保存和检索对象的实例?
- 我该怎么做才能使我的对象序列化而不引发运行时异常?
我发现这个异常特别奇怪,因为我的对象在屏幕旋转时似乎序列化没有问题,但在应用程序关闭时会导致错误。我在下面包含了堆栈跟踪,以防相关。如果需要,我还可以发布相关对象(Board)的代码。在此先感谢您的任何建议!
08-05 18:10:32.322 32059-32059/? E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.RuntimeException: Parcelable encountered IOException writing serializable object (name = com.example.android.Board)
at android.os.Parcel.writeSerializable(Parcel.java:1279)
at android.os.Parcel.writeValue(Parcel.java:1233)
at android.os.Parcel.writeMapInternal(Parcel.java:591)
at android.os.Bundle.writeToParcel(Bundle.java:1627)
at android.os.Parcel.writeBundle(Parcel.java:605)
at android.app.ActivityManagerProxy.activityStopped(ActivityManagerNative.java:2252)
at android.app.ActivityThread$StopInfo.run(ActivityThread.java:3179)
at android.os.Handler.handleCallback(Handler.java:725)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5226)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.io.NotSerializableException: com.example.android.Square
at java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1364)
at java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1671)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1517)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1481)
at java.io.ObjectOutputStream.writeNewArray(ObjectOutputStream.java:1205)
at java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1662)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1517)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1481)
at java.io.ObjectOutputStream.writeNewArray(ObjectOutputStream.java:1205)
at java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1662)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1517)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1481)
at java.io.ObjectOutputStream.writeFieldValues(ObjectOutputStream.java:979)
at java.io.ObjectOutputStream.defaultWriteObject(ObjectOutputStream.java:368)
at java.io.ObjectOutputStream.writeHierarchy(ObjectOutputStream.java:1074)
at java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1404)
at java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1671)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1517)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1481)
at android.os.Parcel.writeSerializable(Parcel.java:1274)
... 15 more