gpt4 book ai didi

java - Android:将可序列化对象传递给 Activity 时出现 ClassNotFoundException

转载 作者:搜寻专家 更新时间:2023-10-30 19:48:37 25 4
gpt4 key购买 nike

我有一些来自 android 市场的崩溃报告日志与此类似:

Exception class: java.lang.ClassNotFoundException

Source method: PathClassLoader.findClass()

java.lang.RuntimeException: Unable to start activity ComponentInfo{my.app.package/my.app.package.MyActivity}: java.lang.RuntimeException: Parcelable encounteredClassNotFoundException reading a Serializable object (name = my.app.package.a.ae)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2833)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2854)
at android.app.ActivityThread.access$2300(ActivityThread.java:136)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2179)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:143)
at android.app.ActivityThread.main(ActivityThread.java:5068)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.RuntimeException: Parcelable encounteredClassNotFoundException reading a Serializable object (name = my.app.package.a.ae)
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.getBundle(Bundle.java:1078)
at android.app.Activity.onRestoreInstanceState(Activity.java:861)
at android.app.Activity.performRestoreInstanceState(Activity.java:835)
at android.app.Instrumentation.callActivityOnRestoreInstanceState(Instrumentation.java:1135)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2811)
... 11 more
Caused by: java.lang.ClassNotFoundException: my.app.package.a.ae
at java.lang.Class.classForName(Native Method)
at java.lang.Class.forName(Class.java:235)
at java.io.ObjectInputStream.resolveClass(ObjectInputStream.java:2590)
at java.io.ObjectInputStream.readNewClassDesc(ObjectInputStream.java:1846)
at java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:826)
at java.io.ObjectInputStream.readNewObject(ObjectInputStream.java:2066)
at java.io.ObjectInputStream.readNonPrimitiveContent(ObjectInputStream.java:929)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:2285)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:2240)
at android.os.Parcel.readSerializable(Parcel.java:1945)
... 19 more
Caused by: java.lang.NoClassDefFoundError: my.app.package.a.ae
... 29 more
Caused by: java.lang.ClassNotFoundException: my.app.package.a.ae in loader dalvik.system.PathClassLoader[.]
at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:243)
at java.lang.ClassLoader.loadClass(ClassLoader.java:573)
at java.lang.ClassLoader.loadClass(ClassLoader.java:532)
... 29 more

请注意,我正在使用混淆器 -> 这就是为什么“缺失的”类名称是“a.ae”的原因。 实际上,“a.ae”是 proguard 给类“MyObject”的名称,如下所示。

知道为什么会这样吗?

我不认为这与混淆器有关,因为如果它是由混淆器类重命名引起的问题,它应该一直发生,我自己可以重现这个问题。

是否与将可序列化对象传递给 MyActivity 有关?
在 MyActivity 中我有

public void onCreate(Bundle savedInstanceState) {  
myObj = (MyObject) getIntent().getSerializableExtra("nameOfMyObject");
//here I am trying to read myObj's properties and app seems to crash here


MyObject 只是一个具有多种类型类成员的实体,例如:
公共(public)类 MyObject 实现可序列化 {

    /** Generated serialVersionUID */
static final long serialVersionUID = -267025879233327409L;

static final String CONSTANT1 = "xpto";

LinkedHashMap<String, Object> components;

ArrayList<String> prop1;

(...)
}

我正在启动 MyActivity:

MyObject obj = new MyObj(stuff);
Intent intent;
intent = new Intent(CurrentActivity.this, MyActivity.class);
intent.putExtra("nameOfMyObject", obj);
CurrentActivity.this.startActivity(intent);

您认为这与对象的传递方式有关(作为可序列化的额外项)吗?
如果我找不到任何其他解决方案,我想我会自己将对象“序列化”为 String,然后额外传递 String。你怎么看这件事?

编辑:我曾尝试在模拟器和以下设备中重现此问题,但没有成功:

-摩托罗拉里程碑 2 Android 2.2
-三星 Galaxy Tab Android 2.2
-沃达丰 845 Android 2.1
-HTC Tatoo Android 1.6
-Xperia X10 Android 1.6
-Nexus One Android 2.2
-谷歌 G1 Android 1.5

有什么新想法吗?

最佳答案

最后对这个问题得出结论:是Proguard引起的,或者更具体地说,是因为我没有正确的Proguard配置。

原来 Proguard 正在更改我的 Serializable 的类名,这使得 Class.forName(className) 失败。

我必须重新配置我的 proguard.cfg 文件,添加以下行:

-keep class * implements java.io.Serializable

-keepclassmembers class * implements java.io.Serializable {
static final long serialVersionUID;
private static final java.io.ObjectStreamField[] serialPersistentFields;
!static !transient <fields>;
!private <fields>;
!private <methods>;
private void writeObject(java.io.ObjectOutputStream);
private void readObject(java.io.ObjectInputStream);
java.lang.Object writeReplace();
java.lang.Object readResolve();
}

关于java - Android:将可序列化对象传递给 Activity 时出现 ClassNotFoundException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6014806/

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