我在将自定义类传递给另一个活动时遇到问题,导致活动管理器崩溃。我有一个这样的课:
public class MyClass implements Serializable/Parcelable {
public final String a;
public final Long b;
public final Boolean c;
}
Intent intent = new Intent(getActivity(), TargetActivity.class);
Bundle parameter = new Bundle();
parameter.putParcelable(TargetActivity.DATA_KEY, instance_of_my_class);
startActivity(intent, parameter);
我同时尝试了Serializable
和Parcelable
两种方法都遇到了同样的问题:ClassNotFoundException
。
有一些较早的讨论,建议使用setClassLoader
和setExtrasClassLoader
,遗憾的是它不起作用。例如,我尝试了parameter.setClassLoader(getClass().getClassLoader());
,结果是一样的。
有谁知道解决方案?第三方图书馆是否可能对此有影响? (我在项目中有EventBus)
Parcelable案例中的堆栈跟踪如下所示:
08-23 14:40:37.086 1487-1783/system_process E/Parcel: Class not found when unmarshalling: com.example.MyClass
java.lang.ClassNotFoundException: com.example.MyClass
at java.lang.Class.classForName(Native Method)
at java.lang.Class.forName(Class.java:309)
at java.lang.Class.forName(Class.java:273)
at android.os.Parcel.readParcelableCreator(Parcel.java:2281)
at android.os.Parcel.readParcelable(Parcel.java:2245)
at android.os.Parcel.readValue(Parcel.java:2152)
at android.os.Parcel.readArrayMapInternal(Parcel.java:2485)
at android.os.BaseBundle.unparcel(BaseBundle.java:221)
at android.os.BaseBundle.getString(BaseBundle.java:918)
at android.app.ActivityOptions.<init>(ActivityOptions.java:570)
at com.android.server.am.ActivityRecord.<init>(ActivityRecord.java:417)
at com.android.server.am.ActivityStackSupervisor.startActivityLocked(ActivityStackSupervisor.java:1482)
at com.android.server.am.ActivityStackSupervisor.startActivityMayWait(ActivityStackSupervisor.java:951)
at com.android.server.am.ActivityManagerService.startActivityAsUser(ActivityManagerService.java:3369)
at com.android.server.am.ActivityManagerService.startActivity(ActivityManagerService.java:3356)
at android.app.ActivityManagerNative.onTransact(ActivityManagerNative.java:139)
at com.android.server.am.ActivityManagerService.onTransact(ActivityManagerService.java:2208)
at android.os.Binder.execTransact(Binder.java:446)
Caused by: java.lang.ClassNotFoundException: Didn't find class "com.example.MyClass" on path: DexPathList[[directory "."],nativeLibraryDirectories=[/vendor/lib, /system/lib]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
at java.lang.ClassLoader.loadClass(ClassLoader.java:469)
at java.lang.Class.classForName(Native Method)
at java.lang.Class.forName(Class.java:309)
at java.lang.Class.forName(Class.java:273)
at android.os.Parcel.readParcelableCreator(Parcel.java:2281)
at android.os.Parcel.readParcelable(Parcel.java:2245)
at android.os.Parcel.readValue(Parcel.java:2152)
at android.os.Parcel.readArrayMapInternal(Parcel.java:2485)
at android.os.BaseBundle.unparcel(BaseBundle.java:221)
at android.os.BaseBundle.getString(BaseBundle.java:918)
at android.app.ActivityOptions.<init>(ActivityOptions.java:570)
at com.android.server.am.ActivityRecord.<init>(ActivityRecord.java:417)
at com.android.server.am.ActivityStackSupervisor.startActivityLocked(ActivityStackSupervisor.java:1482)
at com.android.server.am.ActivityStackSupervisor.startActivityMayWait(ActivityStackSupervisor.java:951)
at com.android.server.am.ActivityManagerService.startActivityAsUser(ActivityManagerService.java:3369)
at com.android.server.am.ActivityManagerService.startActivity(ActivityManagerService.java:3356)
at android.app.ActivityManagerNative.onTransact(ActivityManagerNative.java:139)
at com.android.server.am.ActivityManagerService.onTransact(ActivityManagerService.java:2208)
at android.os.Binder.execTransact(Binder.java:446)
Suppressed: java.lang.ClassNotFoundException: com.example.MyClass
at java.lang.Class.classForName(Native Method)
at java.lang.BootClassLoader.findClass(ClassLoader.java:781)
at java.lang.BootClassLoader.loadClass(ClassLoader.java:841)
at java.lang.ClassLoader.loadClass(ClassLoader.java:504)
... 19 more
Caused by: java.lang.NoClassDefFoundError: Class not found using the boot class loader; no stack available
答案 0 :(得分:0)
实现Parcelable接口的类还必须具有一个名为CREATOR的非空静态字段,该字段实现Parcelable.Creator接口。
参考:https://developer.android.com/reference/android/os/Parcelable.html
要以简单的方式做到这一点,尝试找到可以处理parcelable创建的插件。 例: https://github.com/mcharmas/android-parcelable-intellij-plugin