保存我的序列化类,该类尚未序列化对象(如 Rect)

发布于 2024-12-09 12:55:50 字数 125 浏览 1 评论 0原文

当活动调用 onDestroy() 时,我试图保存序列化对象,但是当我尝试使用 ObjectOutputStream 写入对象时,会引发 java.io.NotSerializedExeption。

你能帮我一下吗?谢谢

I'm trying to save my Serialized object when the activity calls the onDestroy() but when i try to write my object using ObjectOutputStream a java.io.NotSerializableExeption is thrown.

Can you please help me. Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

绮烟 2024-12-16 12:55:51

我遇到了同样的问题并制作了这个类来帮助序列化:

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);
}
}

I encountered same problem and made this class to help the serialization:

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);
}
}
情话已封尘 2024-12-16 12:55:51

如果您的对象映射包含不可序列化的对象,VM 将查找无参数构造函数,以便创建具有默认值的对象并继续。当它不能时,它会抛出此异常。最重要的是,对象图中引用的每个对象都必须:

  1. 实现可序列化(并且它的所有成员也这样做),或者
  2. 从实现可序列化的基类继承(并且所有成员都遵循规则),或者
  3. 实现可序列化和所有不可序列化成员都标记为易失性,或者
  4. 未如此标记的不可序列化成员具有用于反序列化的无参数默认构造函数。

您没有提供足够的细节让我进一步了解,但我无论如何也无法为您写出来?一般来说,您可以从不可序列化的类派生或包装它们并提供必要的功能。只要每个成员都满足我列出的条件,就不应抛出异常。

If your object map contains an unserializable object the VM looks for a no argument constructor so as to make an object with the default values and move on. It throws this exception when it can't. The bottom line is that each and every object referenced in your object graph must either:

  1. Implements Serializable, (and all of it's members do too), or
  2. Inherits from a base class that implements Serializable(and all members follow the rules), or
  3. Implements Serializable and all unserializable members are marked as Volatile, or
  4. Unserializable members not so marked have a no-argument default constructor for use in deserialization.

You do not provide enough specifics for me to go further, but I can't write it for you anyway? In general, you can derive from the unserializable classes or wrap them and provide the necessary functionality. So long as each member meets the conditions I listed, no exception should be thrown.

流年已逝 2024-12-16 12:55:51

您需要确保该类实现 java.io.Serialized 接口。还可以考虑使用 Bundles 并将数据保存在 onSaveInstanceState 中。有关数据存储的更多信息,请参阅此开发指南主题

You need to ensure that the class implements the java.io.Serializable interface. Also consider using Bundles instead and saving your data in onSaveInstanceState. For more on data storage, see this dev guide topic.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文