我的应用程序因为切换而暂停状态时遇到问题。我重载了onSaveInstanceState和onRestoreInstanceState方法,以保存和恢复唯一包含对我的应用程序状态(由一堆数组,arraylists,hashsets,枚举和基元组成)重要的变量的对象。不幸的是,切换到我的应用程序会导致它因序列化错误而崩溃。这是我保存和恢复状态的代码:
@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,因此我不知道还需要做些什么来使其可序列化。通常这就是必要的 我有两个问题:
我发现这个异常特别奇怪,因为我的对象在旋转屏幕时似乎没有问题序列化,但在应用程序关闭时会导致错误。我已经在下面列出了堆栈跟踪,以防它相关。如果需要,我也可以从相关对象(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
答案 0 :(得分:-1)
正如drurenia在评论中指出的那样,我需要将implements Serializable
添加到我的Square类中,这是我的Board类中包含的成员之一。
我的第二个问题是由Square类中的循环引用引起的,因为每个Square包含对其每个邻居的引用。它应该可以通过在Board类中实现readObject
和writeObject
来解决。有关在序列化期间处理循环引用的更多信息,请参见in this answer。