Adam Varhegyi Adam Varhegyi - 4 months ago 34
Java Question

Class not found when unmarshalling

Im trying to pass objects between fragments, and i use Parcelable interface.

Here is my class but something is messed up with the ArrayList property, as i got:

java.lang.ClassNotFoundException: hu.company.myapp.modul.RaceDayUnit


and my class has an ArrayList of RaceDayUnits. All was worked properly until i started to use ArrayList.

Please help if you can.

My class:

public class RegattaUnit implements Parcelable {

public String id;
public String name;
public String type;
public String status;
public String token;

public ArrayList<RaceDayUnit> raceDays = new ArrayList<RaceDayUnit>();


public static class Status {
public static String NEW = "invited";
public static String JOINED = "joined";
public static String DECLINED = "declined";
}

public RegattaUnit() {
}

// not used at all
@Override
public int describeContents() {
return 0;
}

@Override
public void writeToParcel(Parcel out, int flags) {
out.writeString(id);
out.writeString(name);
out.writeString(type);
out.writeString(status);
out.writeString(token);
out.writeList(raceDays);
}

private RegattaUnit(Parcel in) {
id = in.readString();
name = in.readString();
type = in.readString();
status = in.readString();
token = in.readString();

raceDays = new ArrayList<RaceDayUnit>();
in.readList(raceDays, null);
}


public static final Creator<RegattaUnit> CREATOR = new Creator<RegattaUnit>() {

public RegattaUnit createFromParcel(Parcel in) {
return new RegattaUnit(in);
}

public RegattaUnit[] newArray(int size) {
return new RegattaUnit[size];
}
};

public RegattaUnit(JSONObject regattaJSONObject, String status) throws JSONException {

//parsing from a jsonObject, the ArrayList is getting filled here.


}




}


}


And log output:

04-27 12:54:23.014: E/Parcel(9257): Class not found when unmarshalling: hu.mycomp.myapp.modul.RaceDayUnit
04-27 12:54:23.014: E/Parcel(9257): java.lang.ClassNotFoundException: hu.mycomp.myapp.modul.RaceDayUnit
04-27 12:54:23.014: E/Parcel(9257): at java.lang.Class.classForName(Native Method)
04-27 12:54:23.014: E/Parcel(9257): at java.lang.Class.forName(Class.java:309)
04-27 12:54:23.014: E/Parcel(9257): at java.lang.Class.forName(Class.java:273)
04-27 12:54:23.014: E/Parcel(9257): at android.os.Parcel.readParcelableCreator(Parcel.java:2281)
04-27 12:54:23.014: E/Parcel(9257): at android.os.Parcel.readParcelable(Parcel.java:2245)
04-27 12:54:23.014: E/Parcel(9257): at android.os.Parcel.readValue(Parcel.java:2152)
04-27 12:54:23.014: E/Parcel(9257): at android.os.Parcel.readListInternal(Parcel.java:2526)
04-27 12:54:23.014: E/Parcel(9257): at android.os.Parcel.readList(Parcel.java:1661)
04-27 12:54:23.014: E/Parcel(9257): at hu.mycomp.myapp.modul.RegattaUnit.<init>(RegattaUnit.java:58)
04-27 12:54:23.014: E/Parcel(9257): at hu.mycomp.myapp.RegattaUnit.<init>(RegattaUnit.java:14)
04-27 12:54:23.014: E/Parcel(9257): at hu.mycomp.myapp.RegattaUnit$1.createFromParcel(RegattaUnit.java:113)
04-27 12:54:23.014: E/Parcel(9257): at hu.mycomp.myapp.RegattaUnit$1.createFromParcel(RegattaUnit.java:110)
04-27 12:54:23.014: E/Parcel(9257): at android.os.Parcel.readParcelable(Parcel.java:2252)
04-27 12:54:23.014: E/Parcel(9257): at android.os.Parcel.readValue(Parcel.java:2152)
04-27 12:54:23.014: E/Parcel(9257): at android.os.Parcel.readArrayMapInternal(Parcel.java:2485)
04-27 12:54:23.014: E/Parcel(9257): at android.os.BaseBundle.unparcel(BaseBundle.java:221)
04-27 12:54:23.014: E/Parcel(9257): at android.os.BaseBundle.getString(BaseBundle.java:918)
04-27 12:54:23.014: E/Parcel(9257): at hu.mycomp.myapp.service.GpsService.onStartCommand(GpsService.java:117)
04-27 12:54:23.014: E/Parcel(9257): at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2894)
04-27 12:54:23.014: E/Parcel(9257): at android.app.ActivityThread.access$2100(ActivityThread.java:151)
04-27 12:54:23.014: E/Parcel(9257): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1401)
04-27 12:54:23.014: E/Parcel(9257): at android.os.Handler.dispatchMessage(Handler.java:102)
04-27 12:54:23.014: E/Parcel(9257): at android.os.Looper.loop(Looper.java:135)
04-27 12:54:23.014: E/Parcel(9257): at android.app.ActivityThread.main(ActivityThread.java:5254)
04-27 12:54:23.014: E/Parcel(9257): at java.lang.reflect.Method.invoke(Native Method)
04-27 12:54:23.014: E/Parcel(9257): at java.lang.reflect.Method.invoke(Method.java:372)
04-27 12:54:23.014: E/Parcel(9257): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
04-27 12:54:23.014: E/Parcel(9257): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
04-27 12:54:23.014: E/Parcel(9257): Caused by: java.lang.ClassNotFoundException: Didn't find class "hu.mycomp.myapp.modul.RaceDayUnit" on path: DexPathList[[directory "."],nativeLibraryDirectories=[/vendor/lib, /system/lib]]
04-27 12:54:23.014: E/Parcel(9257): at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
04-27 12:54:23.014: E/Parcel(9257): at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
04-27 12:54:23.014: E/Parcel(9257): at java.lang.ClassLoader.loadClass(ClassLoader.java:469)
04-27 12:54:23.014: E/Parcel(9257): ... 28 more
04-27 12:54:23.014: E/Parcel(9257): Suppressed: java.lang.ClassNotFoundException: hu.mycomp.myapp.modul.RaceDayUnit
04-27 12:54:23.014: E/Parcel(9257): at java.lang.Class.classForName(Native Method)
04-27 12:54:23.014: E/Parcel(9257): at java.lang.BootClassLoader.findClass(ClassLoader.java:781)
04-27 12:54:23.014: E/Parcel(9257): at java.lang.BootClassLoader.loadClass(ClassLoader.java:841)
04-27 12:54:23.014: E/Parcel(9257): at java.lang.ClassLoader.loadClass(ClassLoader.java:504)
04-27 12:54:23.014: E/Parcel(9257): ... 29 more
04-27 12:54:23.014: E/Parcel(9257): Caused by: java.lang.NoClassDefFoundError: Class not found using the boot class loader; no stack available
04-27 12:54:23.015: D/AndroidRuntime(9257): Shutting down VM
04-27 12:54:23.021: E/AndroidRuntime(9257): FATAL EXCEPTION: main
04-27 12:54:23.021: E/AndroidRuntime(9257): Process: hu.mycomp.myapp, PID: 9257
04-27 12:54:23.021: E/AndroidRuntime(9257): java.lang.RuntimeException: Unable to start service hu.mycomp.myapp.service.GpsService@16aad1da with Intent { flg=0x4 cmp=hu.mycomp.myapp/.service.GpsService (has extras) }: android.os.BadParcelableException: ClassNotFoundException when unmarshalling: hu.mycomp.myapp.modul.RaceDayUnit
04-27 12:54:23.021: E/AndroidRuntime(9257): at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2911)
04-27 12:54:23.021: E/AndroidRuntime(9257): at android.app.ActivityThread.access$2100(ActivityThread.java:151)
04-27 12:54:23.021: E/AndroidRuntime(9257): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1401)
04-27 12:54:23.021: E/AndroidRuntime(9257): at android.os.Handler.dispatchMessage(Handler.java:102)
04-27 12:54:23.021: E/AndroidRuntime(9257): at android.os.Looper.loop(Looper.java:135)
04-27 12:54:23.021: E/AndroidRuntime(9257): at android.app.ActivityThread.main(ActivityThread.java:5254)
04-27 12:54:23.021: E/AndroidRuntime(9257): at java.lang.reflect.Method.invoke(Native Method)
04-27 12:54:23.021: E/AndroidRuntime(9257): at java.lang.reflect.Method.invoke(Method.java:372)
04-27 12:54:23.021: E/AndroidRuntime(9257): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
04-27 12:54:23.021: E/AndroidRuntime(9257): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
04-27 12:54:23.021: E/AndroidRuntime(9257): Caused by: android.os.BadParcelableException: ClassNotFoundException when unmarshalling: hu.mycomp.myapp.modul.RaceDayUnit
04-27 12:54:23.021: E/AndroidRuntime(9257): at android.os.Parcel.readParcelableCreator(Parcel.java:2295)
04-27 12:54:23.021: E/AndroidRuntime(9257): at android.os.Parcel.readParcelable(Parcel.java:2245)
04-27 12:54:23.021: E/AndroidRuntime(9257): at android.os.Parcel.readValue(Parcel.java:2152)
04-27 12:54:23.021: E/AndroidRuntime(9257): at android.os.Parcel.readListInternal(Parcel.java:2526)
04-27 12:54:23.021: E/AndroidRuntime(9257): at android.os.Parcel.readList(Parcel.java:1661)
04-27 12:54:23.021: E/AndroidRuntime(9257): at hu.mycomp.myapp.modul.RegattaUnit.<init>(RegattaUnit.java:58)
04-27 12:54:23.021: E/AndroidRuntime(9257): at hu.mycomp.myapp.modul.RegattaUnit.<init>(RegattaUnit.java:14)
04-27 12:54:23.021: E/AndroidRuntime(9257): at hu.mycomp.myapp.modul.RegattaUnit$1.createFromParcel(RegattaUnit.java:113)
04-27 12:54:23.021: E/AndroidRuntime(9257): at hu.mycomp.myapp.modul.RegattaUnit$1.createFromParcel(RegattaUnit.java:110)
04-27 12:54:23.021: E/AndroidRuntime(9257): at android.os.Parcel.readParcelable(Parcel.java:2252)
04-27 12:54:23.021: E/AndroidRuntime(9257): at android.os.Parcel.readValue(Parcel.java:2152)
04-27 12:54:23.021: E/AndroidRuntime(9257): at android.os.Parcel.readArrayMapInternal(Parcel.java:2485)
04-27 12:54:23.021: E/AndroidRuntime(9257): at android.os.BaseBundle.unparcel(BaseBundle.java:221)
04-27 12:54:23.021: E/AndroidRuntime(9257): at android.os.BaseBundle.getString(BaseBundle.java:918)
04-27 12:54:23.021: E/AndroidRuntime(9257): at hu.mycomp.myapp.service.GpsService.onStartCommand(GpsService.java:117)
04-27 12:54:23.021: E/AndroidRuntime(9257): at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2894)
04-27 12:54:23.021: E/AndroidRuntime(9257): ... 9 more

Answer

Rather than passing in null to readList() as the second parameter, pass in the ClassLoader that has RaceUnit. If RegattaUnit and RaceUnit are both in the same app and DEX file, getClass().getClassLoader() or RaceUnit.class.getClassLoader() should work.

(and now I am wondering what the SI unit of regatta actually is... :-)

Comments