2

アクティビティが onDestroy() を呼び出すときにシリアライズされたオブジェクトを保存しようとしていますが、ObjectOutputStream を使用してオブジェクトを書き込もうとすると、java.io.NotSerializableExeption がスローされます。

手伝ってくれませんか。ありがとう

4

3 に答える 3

1

私は同じ問題に遭遇し、シリアライゼーションを支援するためにこのクラスを作成しました:

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);
}
}
于 2013-09-05T09:12:17.437 に答える
-1

クラスがインターフェイスを実装していることを確認する必要がありjava.io.Serializableます。また、代わりにバンドルを使用し、データを に保存することも検討してくださいonSaveInstanceState。データ ストレージの詳細については、この開発者ガイドのトピックを参照してください。

于 2011-10-11T00:15:34.040 に答える