我正在尝试使用以下方法对 Pair 进行序列化:
private void writeObject(java.io.ObjectOutputStream out) throws IOException {
if (mPair != null) {
String first = mPair.first;
String second = mPair.second;
mPair = null;
try {
out.writeChars(first);
out.writeChars("\n");
out.writeChars(second);
} catch (Exception e) {
}
}
}
private void readObject(java.io.ObjectInputStream in) throws IOException,
ClassNotFoundException {
try {
String first = in.readLine();
String second = in.readLine();
mPair = new Pair<String, String>(first, second);
} catch (EOFException e) {
mPair = new Pair<String, String>("", "");
}
}
当我的应用程序离开屏幕时,我调试它writeObject
被正确调用,因为我有 3 个自定义类对象,但是当我回到应用程序时,readObject
从来没有被调用过。