我试图在片段之间传递对象,并且我使用Parcelable接口。
这是我的课程,但是我得到的ArrayList属性有些混乱:
java.lang.ClassNotFoundException: hu.company.myapp.modul.RaceDayUnit
我的班级有一个RaceDayUnits的ArrayList。一切工作正常,直到我开始使用ArrayList。
如果可以的话请帮忙。
我的课:
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.
}
}
}
并输出日志:
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
最佳答案
而不是将null
传递给readList()
作为第二个参数,而是传递具有ClassLoader
的RaceUnit
。如果RegattaUnit
和RaceUnit
都在同一个应用程序和DEX文件中,则getClass().getClassLoader()
或RaceUnit.class.getClassLoader()
应该起作用。
(现在,我想知道帆船赛的国际单位制实际上是... :-)