【发布时间】:2017-01-01 00:29:25
【问题描述】:
在我的 Android 应用程序中,我有一个自定义对象列表
ArrayList<Poke> pcList = new ArrayList<>();
稍后在我的代码中,我有保存和加载方法,它们使用 Java OOS 和 OIS 来运行。我在其他项目中使用过这些确切的方法,并且知道它们可以正常工作。
我认为我在保存和加载自定义对象列表时遇到了问题。
这是我调用来保存和加载我的列表的行。
// save
oos.writeObject(pcList);
...
// load
pcList = (ArrayList<Poke>)ois.readObject();
任何想法为什么我不能正确加载/保存我的自定义对象列表?
这里是链接几个相似对象的界面:
public interface Poke {
int getNum();
String getName();
String getType();
int getValue();
boolean getShiny();
String getImageName();
}
这是类似的对象类之一
public class BasicPoke implements Poke {
boolean shiny = false;
Random randomInt = new Random();
int iRandom = randomInt.nextInt(34 - 1) + 1;
int iShiny = randomInt.nextInt(31 - 1) + 1;
public int getNum(){
return this.iRandom;
}
public String getName(){
return this.pokeNames[iRandom-1];
}
public String getType(){
return this.pokeTypes[iRandom-1];
}
public int getValue(){
return 1;
}
public boolean getShiny() {
return (iShiny == 15);
}
public String getImageName() {
String threeDigitInteger = String.format(Locale.getDefault(),"%03d", this.getNum());
return (this.getShiny()) ? "icon"+threeDigitInteger+"s" : "icon"+threeDigitInteger;
}
String pokeNames = {"..","..",".."};
String pokeTypes = {"..","..",".."};
【问题讨论】:
-
你的自定义课程
implements Serializable? -
@Zircon 他们实现了我的界面
-
但是他们没有实现Serializable。您将需要这样做。不幸的是,这并不总是最容易做到的。我建议您阅读 Joshua Bloch 的《Effective Java: Second Edition》一书,并阅读他关于正确实现 Serializable 接口的章节。正确实现 Serializable 后,在文件系统上读取/写入对象将变得轻而易举。
标签: java android objectinputstream objectoutputstream