gpt4 book ai didi

android - 在 Android 中读取序列化文件时出现 EOFException

转载 作者:行者123 更新时间:2023-11-30 03:05:15 24 4
gpt4 key购买 nike

我一直在寻找这个问题的答案,但其他答案都不适合我。我在 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

此外,即使这只是警告,对象也不会加载,所以我无法使用它。

最佳答案

不知道能不能帮到你,here它说 Android 中的序列化可能存在大量整数数组的问题。在您的例子中,您有一个 bool 值数组,无论如何它都是一个整数。

我会尝试通过剥离一些可序列化数据(例如,仅序列化部分信息)来进行测试,以查看问题是否与数组有关。

此外,这可能是显而易见的,但也可能不是……确保您没有加载已使用不同版本的对象保存的文件(例如,您保存对象进行一些测试,然后添加变量到你的类(class),现在你正在尝试反序列化)。

关于android - 在 Android 中读取序列化文件时出现 EOFException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21918154/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com