当我将Parcelable对象的arraylist传递给GCM服务时,我得到了异常。 非常感谢帮助。
这不是其他一些问题(如this one)的情况,他们在一个parcelable对象中有一个ArrayList。我有一个parcelable对象的ArrayList
我有以下Parcelable类:
public class PreferencesLeg implements Parcelable {
public int index;
public int pref1;
public int pref2;
public PreferencesLeg(int index, int pref1, int pref2) {
this.index = index;
this.pref1 = pref1;
this.pref2 = pref2;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(this.index);
dest.writeInt(this.pref1);
dest.writeInt(this.pref2);
}
protected PreferencesLeg(Parcel in) {
this.index = in.readInt();
this.pref1 = in.readInt();
this.pref2 = in.readInt();
}
public static final Creator<PreferencesLeg> CREATOR = new Creator<PreferencesLeg>() {
public PreferencesLeg createFromParcel(Parcel source) {
return new PreferencesLeg(source);
}
public PreferencesLeg[] newArray(int size) {
return new PreferencesLeg[size];
}
};
}
我正在使用此课程如下:
Bundle extras = new Bundle();
ArrayList<PreferencesLeg> preferencesLegs = new ArrayList<>();
for (Leg leg : legList.legs) {
PreferencesLeg preferencesLeg = new PreferencesLeg(leg.index, leg.pref1,
leg.pref2);
preferencesLegs.add(preferencesLeg);
}
extras.putParcelableArrayList(PREFERENCES_LEGS, preferencesLegs);
OneoffTask oneoffTask = new OneoffTask.Builder()
.setService(UpdatePreferencesService.class)
.setTag(unique_id)
.setExtras(extras)
.setExecutionWindow(0.0F, 10.0F)
.setRequiredNetwork(Task.NETWORK_STATE_CONNECTED)
.setUpdateCurrent(true)
.build();
GcmNetworkManager.getInstance(this).schedule(oneoffTask);
...
public class UpdatePreferencesService extends GcmTaskService {
@Override
public int onRunTask(TaskParams taskParams) {
String id = taskParams.getTag();
Bundle extras = taskParams.getExtras();
ArrayList<PreferencesLeg> preferencesLegs = extras.getParcelableArrayList(PREFERENCES_LEGS);
...
我尝试运行任务时遇到异常:
10-27 16:26:12.158 10934-10934/com.google.process.gapps E/Parcel﹕ Class not found when unmarshalling: com.models.PreferencesLeg
java.lang.ClassNotFoundException: com.models.PreferencesLeg
at java.lang.Class.classForName(Native Method)
at java.lang.Class.forName(Class.java:309)
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.readListInternal(Parcel.java:2526)
at android.os.Parcel.readArrayList(Parcel.java:1842)
at android.os.Parcel.readValue(Parcel.java:2173)
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 com.google.android.gms.gcm.nts.a.<init>(SourceFile:101)
at com.google.android.gms.gcm.nts.a.<init>(SourceFile:81)
at com.google.android.gms.gcm.nts.h.a(SourceFile:55)
at com.google.android.gms.gcm.nts.i.handleMessage(SourceFile:170)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Caused by: java.lang.ClassNotFoundException: Didn't find class "com.models.PreferencesLeg" on path: DexPathList[[zip file "/system/framework/com.android.location.provider.jar", zip file "/system/framework/com.android.media.remotedisplay.jar", zip file "/data/app/com.google.android.gms-2/base.apk"],nativeLibraryDirectories=[/data/app/com.google.android.gms-2/lib/x86, /data/app/com.google.android.gms-2/lib/x86, /data/app/com.google.android.gms-2/lib/x86, /data/app/com.google.android.gms-2/lib/x86, /data/app/com.google.android.gms-2/lib/x86, /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 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.readListInternal(Parcel.java:2526)
at android.os.Parcel.readArrayList(Parcel.java:1842)
at android.os.Parcel.readValue(Parcel.java:2173)
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 com.google.android.gms.gcm.nts.a.<init>(SourceFile:101)
at com.google.android.gms.gcm.nts.a.<init>(SourceFile:81)
at com.google.android.gms.gcm.nts.h.a(SourceFile:55)
at com.google.android.gms.gcm.nts.i.handleMessage(SourceFile:170)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Suppressed: java.lang.ClassNotFoundException: com...models.PreferencesLeg
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)
... 23 more
Caused by: java.lang.NoClassDefFoundError: Class not found using the boot class loader; no stack available
10-27 16:26:12.158 10934-10934/com.google.process.gapps D/AndroidRuntime﹕ Shutting down VM
10-27 16:26:12.158 10934-10934/com.google.process.gapps E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.google.process.gapps, PID: 10934
android.os.BadParcelableException: ClassNotFoundException when unmarshalling: com.models.PreferencesLeg
at android.os.Parcel.readParcelableCreator(Parcel.java:2295)
at android.os.Parcel.readParcelable(Parcel.java:2245)
at android.os.Parcel.readValue(Parcel.java:2152)
at android.os.Parcel.readListInternal(Parcel.java:2526)
at android.os.Parcel.readArrayList(Parcel.java:1842)
at android.os.Parcel.readValue(Parcel.java:2173)
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 com.google.android.gms.gcm.nts.a.<init>(SourceFile:101)
at com.google.android.gms.gcm.nts.a.<init>(SourceFile:81)
at com.google.android.gms.gcm.nts.h.a(SourceFile:55)
at com.google.android.gms.gcm.nts.i.handleMessage(SourceFile:170)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
答案 0 :(得分:0)
您可以尝试直接设置ClassLoader。
extras.setClassLoader(PreferencesLeg.class.getClassLoader());