Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/234.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android:ObjectInputStream在读取arraylist时抛出ClassCastException_Android_Arraylist_Classcastexception_Objectinputstream - Fatal编程技术网

Android:ObjectInputStream在读取arraylist时抛出ClassCastException

Android:ObjectInputStream在读取arraylist时抛出ClassCastException,android,arraylist,classcastexception,objectinputstream,Android,Arraylist,Classcastexception,Objectinputstream,我有一个实现serializable的特定POJO对象的arraylist。当我将对象写入磁盘时,我是这样做的: File outputDir = context.getCacheDir(); File outputFile = new File(outputDir, getCacheKey() ); ObjectOutputStream oos = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream(out

我有一个实现serializable的特定POJO对象的arraylist。当我将对象写入磁盘时,我是这样做的:

File outputDir = context.getCacheDir();
File outputFile = new File(outputDir,  getCacheKey() );
ObjectOutputStream oos = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream(outputFile)));
oos.writeObject(arraylistOfMyObjects);
oos.close();
File outputDir = context.getCacheDir();
File outputFile = new File(outputDir,  getCacheKey() );
ObjectInputStream ois = new ObjectInputStream(new BufferedInputStream(new FileInputStream(outputFile)));
final ArrayList<myObject> list = (ArrayList<myObject>) ois.readObject();
ois.close();
当我以后想把这个列表读回对象的arraylist时,我是这样做的:

File outputDir = context.getCacheDir();
File outputFile = new File(outputDir,  getCacheKey() );
ObjectOutputStream oos = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream(outputFile)));
oos.writeObject(arraylistOfMyObjects);
oos.close();
File outputDir = context.getCacheDir();
File outputFile = new File(outputDir,  getCacheKey() );
ObjectInputStream ois = new ObjectInputStream(new BufferedInputStream(new FileInputStream(outputFile)));
final ArrayList<myObject> list = (ArrayList<myObject>) ois.readObject();
ois.close();
为什么会发生这些错误?我如何更改代码以解决这些错误


有趣的是,我并不是每次运行应用程序时都会出现错误,只是偶尔会因为奇怪的原因出现错误。

确保每次都以相同的顺序写回和读取对象。在我的例子中,我意识到我正在使用如下可选参数写入文件:

java.lang.ClassCastException: java.util.ArrayList cannot be cast to java.io.ObjectStreamClass
if(cacheAllItems) { // <-- BREAKS READING FILE WHEN FALSE
   objectOutputStream.writeObject(itemsList);
   objectOutputStream.writeObject(itemNamesList);
}
objectOutputStream.writeObject(currentItem);
然后,我尝试使用以下命令读取文件:

itemsList = (ArrayList<Item>) objectInputStream.readObject(); // MIGHT NOT EXIST
itemNames = (ArrayList<String>) objectInputStream.readObject(); // MIGHT NOT EXIST
currentItem = (Item) objectInputStream.readObject();

因此,如果上次运行cacheAllItems=true的cacheItems函数,一切正常,但如果上次运行的只是保存currentItem,readObject会尝试将一个对象作为应该首先出现的列表来读取。

myObject几乎完全由基本类型Int、String等组成。,但它拥有的少数对象都实现了可序列化。我已经仔细检查了如果我处在你的位置,我会尝试两种不同的情况:1。尝试将myObject更改为integer之类的基本类类型,并发送数组2。尝试发送一个MyObject而不是数组,然后可以看到异常在哪里。