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
georgij
  • 2,054
  • 4
  • 27
  • 43
  • 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 – OneCricketeer Aug 23 '16 at 12:55
  • So where is the constructor that takes PARCEL and the CREATOR? – EpicPandaForce Aug 23 '16 at 12:57
  • please add full code. – Sohail Zahid Aug 23 '16 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. – georgij Aug 23 '16 at 13:07
  • Random guess. Is the class package name matching your applicationId? – OneCricketeer Aug 23 '16 at 13:26
  • No, the class which is not found is a bit deeper in the package structure so the applicationId is only the prefix of its package name. – georgij Aug 23 '16 at 13:35
  • might be connected with this issue : http://stackoverflow.com/questions/22473348/class-not-found-when-unmarshalling-android-intent-parcelable – Klawikowski Aug 23 '16 at 13:40
  • Please show the code where you are putting the custom class instances into the `Intent` and what you are doing with this `Intent`. – David Wasser Aug 23 '16 at 15:14
  • Possible duplicate of [Unmarshalling errors in Android app with custom parcelable classes](http://stackoverflow.com/questions/13997550/unmarshalling-errors-in-android-app-with-custom-parcelable-classes) – user1643723 Aug 23 '16 at 22:07
  • possible duplitcate of http://stackoverflow.com/questions/33967703/unable-to-instantiate-application-com-android-tools-fd-runtime-bootstrapapplicat – peeyush pathak Aug 24 '16 at 09:03

1 Answers1

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

Klawikowski
  • 605
  • 3
  • 11