我已经实现了一个实现可序列化对象的类。
public class SaveMe implements Serializable {
private static final long serialVersionUID = 1L;
private String someValue1;
private String someValue2;
}
但是每当我尝试在 Bundle 中使用它时,我都会收到此异常:
Parcelable encounteredClassNotFoundException reading a Serializable object
at android.os.Parcel.readSerializable(Parcel.java:1951)
at android.os.Parcel.readValue(Parcel.java:1822)
at android.os.Parcel.readMapInternal(Parcel.java:2008)
at android.os.Bundle.unparcel(Bundle.java:208)
at android.os.Bundle.getString(Bundle.java:1034)
我做错了什么?
编辑
嗯...使用 Parcelable 时出现类似问题:
Class not found when unmarshalling
最佳答案
我相信您可能遇到了类加载器问题。在从中读取序列化对象之前,您需要在 Bundle 上调用 setClassLoader()。
确保在从包中读取之前调用 setClassLoader(SaveMe.class.getClassLoader())
。
如果编译器在编译时找不到 SaveMe,则可以肯定目标应用程序中缺少该类,如 Damp建议。
有关更多详细信息,请参阅此答案: Parcelable inside bundle which is added to Parcel
关于java - Parcelable 读取 Serialized 对象时遇到 ClassNotFoundException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4867582/