我試圖保存我的序列化對象時,活動調用onDestroy()但當我嘗試使用ObjectOutputStream寫入我的對象時拋出java.io.NotSerializableExeption。保存我沒有序列化對象的序列化類像Rect
你能幫我嗎。謝謝
我試圖保存我的序列化對象時,活動調用onDestroy()但當我嘗試使用ObjectOutputStream寫入我的對象時拋出java.io.NotSerializableExeption。保存我沒有序列化對象的序列化類像Rect
你能幫我嗎。謝謝
您需要確保該類實現java.io.Serializable
接口。另外考慮使用Bundles,並將您的數據保存在onSaveInstanceState
中。有關數據存儲的更多信息,請參閱this dev guide topic。
我遇到同樣的問題,使這個類來幫助序列:
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);
}
}
如果你的對象地圖中包含一個不可序列化對象的VM尋找一個無參數的構造函數,使用默認值的對象並繼續前進。它不能引發這個異常。底線是,每一個對象在你的對象圖引用必須:
你沒有提供足夠的細節讓我走得更遠,但是我無法爲你寫出它嗎?通常,您可以從不可序列化的類派生出來,或者將它們包裝並提供必要的功能。只要每個成員符合我列出的條件,就不會拋出異常。
yes我在我的GameField類上實現了java.io.Serializable,我的GameField類包含fileds Rect bouds,當我保存GameField類時拋出java.io.NotSerializableExeption,我認爲它是界限。我的問題是如何存儲我的界限?謝謝 –
好吧,在這種情況下,您需要手動序列化對象。您可以覆蓋'writeObject'和'readObject'方法來執行此操作。 –
請給我一些提示如何去做。謝謝 –