3

I have a problem passing a custom class to another activity resulting in a Activity Manager crash. I have a class like this:

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

I have tried both Serializable as well as Parcelable and both ways fail with the same problem: ClassNotFoundException.

There are some older discussions about this where setClassLoader and setExtrasClassLoader are recommended, unfortunately it does not work. For example I have tried parameter.setClassLoader(getClass().getClassLoader()); the result was the same.

Anyone knows the solution? Is it possible that a third party library has an influence on this? (I have EventBus in the project)

The stack trace in the Parcelable case looks like this:

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
10
  • What exactly is the stacktrace? Which class is not found? And please show the full class definition because you can't just implement Parcelable and have it work Commented Aug 23, 2016 at 12:55
  • So where is the constructor that takes PARCEL and the CREATOR? Commented Aug 23, 2016 at 12:57
  • please add full code. Commented Aug 23, 2016 at 13:00
  • Of case it has the necessary methods implemented and a CREATOR constructor. Just wanted to summarize on the essantials. The stack trace varies depending on if I go the Serializable or the Parcelable way but the meaning is the same. It can't find my class. Commented Aug 23, 2016 at 13:07
  • Random guess. Is the class package name matching your applicationId? Commented Aug 23, 2016 at 13:26

1 Answer 1

0

Classes implementing the Parcelable interface must also have a non-null static field called CREATOR of a type that implements the Parcelable.Creator interface.

Ref: https://developer.android.com/reference/android/os/Parcelable.html

To make that in a easy way try to find plugins which will handle parcelable creation. Example: https://github.com/mcharmas/android-parcelable-intellij-plugin

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.