gpt4 book ai didi

java - InvalidClassException 不兼容的类 (SUID)

转载 作者:行者123 更新时间:2023-12-01 09:51:23 25 4
gpt4 key购买 nike

我正在开发一款 Android 应用程序,我必须在其中保留一些数据。为了持久化它们,我使用了java序列化机制。

这是我正在序列化的类的结构:

public final class A
implements Serializable
{

public static final class B
implements Serializable
{

public final String field1;

public final String field2;

public B(String field1, String field2)
{
this.field1= field1;
this.field2= field2;
}
}

public static final class C
implements Serializable
{

public long field1;

public final B field2;

public C(B field2)
{
this.field2 = field2;
}

public void setField1(long field1)
{
this.field1= field1;
}

}

private final List<C> list1= new ArrayList<>();

private final List<C> list2= new ArrayList<>();

public A()
{

}

public List<C> getList1()
{
return list1;
}

public List<C> getList2()
{
return list2;
}

//some other methods

}

如您所见,类实现 Serializable但我没有指定 serialVersionUID静态字段。

我必须对此应用程序进行重大更新,并从类 A 中删除一些方法和C .

正如预期的那样,当我尝试将旧类反序列化为新类时,出现以下异常:

Caused by: java.io.InvalidClassException: com.my.package.A; Incompatible class (SUID): com.my.package.A: static final long serialVersionUID =9103658319690261655L; but expected com.my.package.A: static final long serialVersionUID =7863476896806312319L; at java.io.ObjectInputStream.verifyAndInit(ObjectInputStream.java:2336) at java.io.ObjectInputStream.readNewClassDesc(ObjectInputStream.java:1643) at java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:657) at java.io.ObjectInputStream.readNewObject(ObjectInputStream.java:1782) at java.io.ObjectInputStream.readNonPrimitiveContent(ObjectInputStream.java:761) at java.io.ObjectInputStream.readObject(ObjectInputStream.java:1983) at java.io.ObjectInputStream.readObject(ObjectInputStream.java:1940)

所以,进入我的类(class)A ,我尝试指定 serialVersionUID具有预期值的静态字段:

private static final long serialVersionUID = 7863476896806312319L;

但是我的应用程序仍然崩溃...我现在有以下堆栈跟踪:

Caused by: java.io.InvalidClassException: com.my.package.A; Incompatible class (SUID): com.my.package.A: static final long serialVersionUID =7863476896806312319L; but expected com.my.package.A: static final long serialVersionUID =9103658319690261655L; at java.io.ObjectInputStream.verifyAndInit(ObjectInputStream.java:2336) at java.io.ObjectInputStream.readNewClassDesc(ObjectInputStream.java:1643) at java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:657) at java.io.ObjectInputStream.readNewObject(ObjectInputStream.java:1782) at java.io.ObjectInputStream.readNonPrimitiveContent(ObjectInputStream.java:761) at java.io.ObjectInputStream.readObject(ObjectInputStream.java:1983) at java.io.ObjectInputStream.readObject(ObjectInputStream.java:1940)

如您所见,旧的serialVersionUID字段现在具有预期值,预期值具有旧值 serialVersionUID领域...

所以问题是:如何恢复我的数据并强制将旧类反序列化为新类?

感谢您的帮助。

最佳答案

查看一个密切相关的问题here ,我认为您应该尝试将 serialVersionUID 设置为 9103658319690261655L 的预期值。

请尝试设置:

private static final long serialVersionUID = 9103658319690261655L;

希望这对您有所帮助,尝试一下并告诉我们。

关于java - InvalidClassException 不兼容的类 (SUID),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37575714/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com