我的一个班级突然开始抱怨它不可序列化。 我已经改变了一些内容,但在其定义中看不到可能导致它的任何内容。
我在那个类上得到了notserializableexception。
这些是课程开始时的所有定义,我看不到任何会导致问题的内容。
private static final long serialVersionUID = 1L;
private ArrayList <Card> cards;
private ArrayList <Card> selectedCards;
private HashSet <Card> knownCards;
private ArrayList <Card> aceHighs;
private int numberOfKnownCards = 0;
private int valueOfKnownCards = 0;
public static final int sortHand = 1;
public static final int sortSelectedCards = 2;
protected int[] cardsPerSuit = {0,0,0,0};
private int numberOfDifferentSuits = 0;
private int[] cardsPerRank = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
private int numberOfDifferentRanks = 0;
private int whereToTakeFrom;
private int whichDiscardToTake;
private int cardCount = 0;
private int selectedCount = 0;
public int handValue = 0;
private Deck deck;
堆栈:
05-08 23:46:42.235: DEBUG/AndroidRuntime(1625): Shutting down VM
05-08 23:46:42.235: WARN/dalvikvm(1625): threadid=3: thread exiting with uncaught exception (group=0x4001b188)
05-08 23:46:42.235: ERROR/AndroidRuntime(1625): Uncaught handler: thread main exiting due to uncaught exception
05-08 23:46:42.265: ERROR/AndroidRuntime(1625): java.lang.RuntimeException: Parcelable encountered IOException writing serializable object (name = il.co.anykey.games.yaniv.lite.AutomatedPlayer)
05-08 23:46:42.265: ERROR/AndroidRuntime(1625): at android.os.Parcel.writeSerializable(Parcel.java:1131)
05-08 23:46:42.265: ERROR/AndroidRuntime(1625): at android.os.Parcel.writeValue(Parcel.java:1085)
05-08 23:46:42.265: ERROR/AndroidRuntime(1625): at android.os.Parcel.writeMapInternal(Parcel.java:469)
05-08 23:46:42.265: ERROR/AndroidRuntime(1625): at android.os.Bundle.writeToParcel(Bundle.java:1445)
05-08 23:46:42.265: ERROR/AndroidRuntime(1625): at android.os.Parcel.writeBundle(Parcel.java:483)
05-08 23:46:42.265: ERROR/AndroidRuntime(1625): at android.app.ActivityManagerProxy.activityPaused(ActivityManagerNative.java:1427)
05-08 23:46:42.265: ERROR/AndroidRuntime(1625): at android.app.ActivityThread.handlePauseActivity(ActivityThread.java:3106)
05-08 23:46:42.265: ERROR/AndroidRuntime(1625): at android.app.ActivityThread.access$2400(ActivityThread.java:119)
05-08 23:46:42.265: ERROR/AndroidRuntime(1625): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1870)
05-08 23:46:42.265: ERROR/AndroidRuntime(1625): at android.os.Handler.dispatchMessage(Handler.java:99)
05-08 23:46:42.265: ERROR/AndroidRuntime(1625): at android.os.Looper.loop(Looper.java:123)
05-08 23:46:42.265: ERROR/AndroidRuntime(1625): at android.app.ActivityThread.main(ActivityThread.java:4363)
05-08 23:46:42.265: ERROR/AndroidRuntime(1625): at java.lang.reflect.Method.invokeNative(Native Method)
05-08 23:46:42.265: ERROR/AndroidRuntime(1625): at java.lang.reflect.Method.invoke(Method.java:521)
05-08 23:46:42.265: ERROR/AndroidRuntime(1625): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
05-08 23:46:42.265: ERROR/AndroidRuntime(1625): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
05-08 23:46:42.265: ERROR/AndroidRuntime(1625): at dalvik.system.NativeStart.main(Native Method)
05-08 23:46:42.265: ERROR/AndroidRuntime(1625): Caused by: java.io.NotSerializableException: il.co.anykey.games.yaniv.lite.Hand$1
05-08 23:46:42.265: ERROR/AndroidRuntime(1625): at java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1547)
05-08 23:46:42.265: ERROR/AndroidRuntime(1625): at java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1854)
05-08 23:46:42.265: ERROR/AndroidRuntime(1625): at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1696)
05-08 23:46:42.265: ERROR/AndroidRuntime(1625): at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1660)
05-08 23:46:42.265: ERROR/AndroidRuntime(1625): at java.io.ObjectOutputStream.writeFieldValues(ObjectOutputStream.java:1153)
05-08 23:46:42.265: ERROR/AndroidRuntime(1625): at java.io.ObjectOutputStream.defaultWriteObject(ObjectOutputStream.java:420)
05-08 23:46:42.265: ERROR/AndroidRuntime(1625): at java.io.ObjectOutputStream.writeHierarchy(ObjectOutputStream.java:1251)
05-08 23:46:42.265: ERROR/AndroidRuntime(1625): at java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1587)
05-08 23:46:42.265: ERROR/AndroidRuntime(1625): at java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1854)
05-08 23:46:42.265: ERROR/AndroidRuntime(1625): at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1696)
05-08 23:46:42.265: ERROR/AndroidRuntime(1625): at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1660)
05-08 23:46:42.265: ERROR/AndroidRuntime(1625): at java.io.ObjectOutputStream.writeFieldValues(ObjectOutputStream.java:1153)
05-08 23:46:42.265: ERROR/AndroidRuntime(1625): at java.io.ObjectOutputStream.defaultWriteObject(ObjectOutputStream.java:420)
05-08 23:46:42.265: ERROR/AndroidRuntime(1625): at java.io.ObjectOutputStream.writeHierarchy(ObjectOutputStream.java:1251)
05-08 23:46:42.265: ERROR/AndroidRuntime(1625): at java.io.ObjectOutputStream.writeHierarchy(ObjectOutputStream.java:1215)
05-08 23:46:42.265: ERROR/AndroidRuntime(1625): at java.io.ObjectOutputStream.writeHierarchy(ObjectOutputStream.java:1215)
05-08 23:46:42.265: ERROR/AndroidRuntime(1625): at java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1587)
05-08 23:46:42.265: ERROR/AndroidRuntime(1625): at java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1854)
05-08 23:46:42.265: ERROR/AndroidRuntime(1625): at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1696)
05-08 23:46:42.265: ERROR/AndroidRuntime(1625): at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1660)
05-08 23:46:42.265: ERROR/AndroidRuntime(1625): at java.io.ObjectOutputStream.writeFieldValues(ObjectOutputStream.java:1153)
05-08 23:46:42.265: ERROR/AndroidRuntime(1625): at java.io.ObjectOutputStream.defaultWriteObject(ObjectOutputStream.java:420)
05-08 23:46:42.265: ERROR/AndroidRuntime(1625): at java.io.ObjectOutputStream.writeHierarchy(ObjectOutputStream.java:1251)
05-08 23:46:42.265: ERROR/AndroidRuntime(1625): at java.io.ObjectOutputStream.writeHierarchy(ObjectOutputStream.java:1215)
05-08 23:46:42.265: ERROR/AndroidRuntime(1625): at java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1587)
05-08 23:46:42.265: ERROR/AndroidRuntime(1625): at java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1854)
05-08 23:46:42.265: ERROR/AndroidRuntime(1625): at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1696)
05-08 23:46:42.265: ERROR/AndroidRuntime(1625): at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1660)
05-08 23:46:42.265: ERROR/AndroidRuntime(1625): at java.io.ObjectOutputStream.writeFieldValues(ObjectOutputStream.java:1153)
05-08 23:46:42.265: ERROR/AndroidRuntime(1625): at java.io.ObjectOutputStream.defaultWriteObject(ObjectOutputStream.java:420)
05-08 23:46:42.265: ERROR/AndroidRuntime(1625): at java.io.ObjectOutputStream.writeHierarchy(ObjectOutputStream.java:1251)
05-08 23:46:42.265: ERROR/AndroidRuntime(1625): at java.io.ObjectOutputStream.writeHierarchy(ObjectOutputStream.java:1215)
05-08 23:46:42.265: ERROR/AndroidRuntime(1625): at java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1587)
05-08 23:46:42.265: ERROR/AndroidRuntime(1625): at java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1854)
05-08 23:46:42.265: ERROR/AndroidRuntime(1625): at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1696)
05-08 23:46:42.265: ERROR/AndroidRuntime(1625): at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1660)
05-08 23:46:42.265: ERROR/AndroidRuntime(1625): at java.io.ObjectOutputStream.write
05-08 23:46:42.316: WARN/ActivityManager(63): Activity pause timeout for HistoryRecord{44d2c128 il.co.anykey.games.yaniv.lite/.YanivGameActivity}
05-08 23:46:42.335: INFO/Process(63): Sending signal. PID: 1625 SIG: 3
05-08 23:46:42.345: INFO/dalvikvm(1625): threadid=7: reacting to signal 3
05-08 23:46:42.396: INFO/dalvikvm(1625): Wrote stack trace to '/data/anr/traces.txt'
05-08 23:46:51.852: WARN/ActivityManager(63): Launch timeout has expired, giving up wake lock!
05-08 23:46:52.395: WARN/ActivityManager(63): Activity idle timeout for HistoryRecord{44d2d728 il.co.anykey.games.yaniv.lite/.YanivSettingsActivity}
这些是我可能产生问题的两个比较器:
private Comparator<Card> byRank = new Comparator<Card>() {
public int compare(Card c1, Card c2) {
return c2.getCardRank()-c1.getCardRank();
}
};
private Comparator<Card> bySuitAndRank = new Comparator<Card>() {
public int compare(Card c1, Card c2) {
return (c1.getCardSuit()-c2.getCardSuit())*100 + c2.getCardRank()-c1.getCardRank();
}
};
答案 0 :(得分:3)
根据堆栈跟踪,类il.co.anykey.games.yaniv.lite.Hand$1
(类Hand
内的匿名类)不可序列化。
答案 1 :(得分:1)
尝试将遇到问题的字段标记为transient
。也许您可以在以后反序列化时重构它们。