我一直在寻找这个问题的答案,但没有其他答案对我有用。我在 android 中使用序列化并序列化一个对象。问题是当我反序列化它时,我得到 EOFException 并且对象不会加载。我的保存代码:
FileOutputStream fileOut = null;
try {
fileOut = context.openFileOutput("state.ser", Context.MODE_PRIVATE);
ObjectOutputStream out = new ObjectOutputStream(fileOut);
out.writeObject(MyObject);
out.close();
fileOut.close();
} catch (IOException i) {
i.printStackTrace();
}
我的加载类抛出该异常:
FileInputStream fis;
try {
fis = context.openFileInput("state.ser");
ObjectInputStream is = new ObjectInputStream(fis);
myObject = (MyObject) is.readObject(); //exception thrown from here
is.close();
} catch (FileNotFoundException e) {
generateMyObject();
} catch (Exception e) {
e.printStackTrace();
}
我的对象的定义:
public class MyObject implements Serializable {
private static final long serialVersionUID = 155986L;
public MyInnerObject[][] mio = null;
public Boolean[] unassigned = null;
}
public class MyInnerObject implements Serializable {
private static final long serialVersionUID = 1L;
public boolean[] legals;
public int ID;
public int pos;
}
我究竟做错了什么?感谢您的所有回答。
编辑:堆栈跟踪
java.io.EOFException
java.io.DataInputStream.readBoolean(DataInputStream.java:69)
java.io.ObjectInputStream.readNewArray(ObjectInputStream.java:1462)
java.io.ObjectInputStream.readNonPrimitiveContent(ObjectInputStream.java:760)
java.io.ObjectInputStream.readObject(ObjectInputStream.java:1981)
java.io.ObjectInputStream.readObject(ObjectInputStream.java:1938)
java.io.ObjectInputStream.readFieldValues(ObjectInputStream.java:1115)
java.io.ObjectInputStream.defaultReadObject(ObjectInputStream.java:455)
java.io.ObjectInputStream.readObjectForClass(ObjectInputStream.java:1347)
java.io.ObjectInputStream.readHierarchy(ObjectInputStream.java:1244)
java.io.ObjectInputStream.readNewObject(ObjectInputStream.java:1833)
java.io.ObjectInputStream.readNonPrimitiveContent(ObjectInputStream.java:762)
java.io.ObjectInputStream.readObject(ObjectInputStream.java:1981)
java.io.ObjectInputStream.readObject(ObjectInputStream.java:1938)
java.io.ObjectInputStream.readNewArray(ObjectInputStream.java:1490)
java.io.ObjectInputStream.readNonPrimitiveContent(ObjectInputStream.java:760)
java.io.ObjectInputStream.readObject(ObjectInputStream.java:1981)
java.io.ObjectInputStream.readObject(ObjectInputStream.java:1938)
java.io.ObjectInputStream.readNewArray(ObjectInputStream.java:1490)
java.io.ObjectInputStream.readNonPrimitiveContent(ObjectInputStream.java:760)
java.io.ObjectInputStream.readObject(ObjectInputStream.java:1981)
java.io.ObjectInputStream.readObject(ObjectInputStream.java:1938)
java.io.ObjectInputStream.readFieldValues(ObjectInputStream.java:1115)
java.io.ObjectInputStream.defaultReadObject(ObjectInputStream.java:455)
java.io.ObjectInputStream.readObjectForClass(ObjectInputStream.java:1347)
java.io.ObjectInputStream.readHierarchy(ObjectInputStream.java:1244)
java.io.ObjectInputStream.readNewObject(ObjectInputStream.java:1833)
java.io.ObjectInputStream.readNonPrimitiveContent(ObjectInputStream.java:762)
java.io.ObjectInputStream.readObject(ObjectInputStream.java:1981)
java.io.ObjectInputStream.readObject(ObjectInputStream.java:1938)
com.example.myApp.Cell.loadState(Cell.java:224)
... rest is only where in app it appeared
即使这只是警告,对象也不会加载,所以我不能使用它。