当活动调用onDestroy()时,我试图保存我的序列化对象,但是当我尝试使用ObjectOutputStream写入对象时,将抛出java.io.NotSerializableExeption。
你能帮我么。谢谢
最佳答案
我遇到了同样的问题,并制作了此类来帮助序列化:
public class SerializableRect implements Serializable {
private static final long serialVersionUID = 1L;
private Rect mRect;
public SerializableRect(Rect rect) {
mRect = rect;
}
public Rect getRect() {
return mRect;
}
private void writeObject(java.io.ObjectOutputStream out) throws IOException {
int left = mRect.left;
int top = mRect.top;
int right = mRect.right;
int bottom = mRect.bottom;
out.writeInt(left);
out.writeInt(top);
out.writeInt(right);
out.writeInt(bottom);
}
private void readObject(java.io.ObjectInputStream in) throws IOException,
ClassNotFoundException {
int left = in.readInt();
int top = in.readInt();
int right = in.readInt();
int bottom = in.readInt();
mRect = new Rect(left, top, right, bottom);
}
}