我有一个似乎无法序列化的自定义类(下面的构造函数)。
public ObjectNode(String name, int crackLevel,ArrayList<ObjectNode> filesOnComputer)
每次我尝试从对象节点的 ArrayList 序列化此类的对象时,我都会得到一个 Class cast 异常。ArrayList 不能转换为 ObjectNode
代码 - 全局变量:
ArrayList<ObjectNode>a=new ArrayList<ObjectNode>();
File file = new File("/mnt/sdcard/","cheesepuff.txt");
相关代码:
public void serializeFile()
{
a.add(new ObjectNode("Level 1 Waterwall","ww1", 1, 5));
try {
Log.i("AAA","before serialize: "+a.toString());
FileOutputStream fos = new FileOutputStream(file);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(a);
oos.flush();
oos.close();
Log.i("AAA","finished serialize");
}
catch(Exception e) {
Log.i("aaa","Exception during serialization: " + e);
System.exit(0);
}
}
public void deserializeFile()
{
try {
FileInputStream fis = new FileInputStream(file);
ObjectInputStream ois = new ObjectInputStream(fis);
ObjectNode obj=(ObjectNode)ois.readObject();
Log.i("aaa","obj: "+obj.ts());
//a.add((ObjectNode) ois.readObject());
ois.close();
Log.i("aaa","after serialize: " + a);
}
catch(Exception e) {
Log.i("aaa","Exception during deserialization: " +
e);
System.exit(0);
}
}
我最好的选择是将所有内容都变成字符串以便对其进行序列化,然后在反序列化后将字符串转换回我实际需要的内容吗?