Android:unmarshalling java.lang.ClassNotFoundException

时间:2016-08-12 19:00:19

标签: android parcelable

我的课程照片列表:

public class PhotosList extends DataList<Photo> {

public PhotosList() {}

@SuppressWarnings("unused")
public PhotosList(Parcel source) {
    super(source);
    items = new ArrayList<>();
    source.readList(items, Photo.class.getClassLoader());
}

@Override
public void writeToParcel(Parcel dest, int flags) {
    super.writeToParcel(dest, flags);
    dest.writeList(items);
}

...

我的班级照片:

public class Photo extends BaseModel {

@SerializedName("photos")
public ArrayList<Image> images;

@SuppressWarnings("unused")
public Photo() {}

@SuppressWarnings("unused")
public Photo(Parcel source) {
    super(source);
    images = new ArrayList<>();
    source.readList(images, Image.class.getClassLoader());
}

@Override
public void writeToParcel(Parcel dest, int flags) {
    super.writeToParcel(dest, flags);
    dest.writeList(images);
}

...

我的班级DataList:

public class DataList<T> implements Parcelable {

@SerializedName("total_count")
public int total;

@SerializedName("message")
public String message;

@SerializedName("items")
public ArrayList<T> items;

@SuppressWarnings("unused")
public DataList() {}

@SuppressWarnings("unused")
public DataList(Parcel source) {
    total = source.readInt();
    message = source.readString();
}

@Override
public void writeToParcel(Parcel dest, int flags) {
    dest.writeInt(total);
    dest.writeString(message);
}

我有这个错误:

  

解组时找不到类:com.xxx.model.PhotosList                                            java.lang.ClassNotFoundException:com.xxx.model.PhotosList                                                at java.lang.Class.classForName(Native Method)                                                at java.lang.Class.forName(Class.java:324)                                                在android.os.Parcel.readParcelableCreator(Parcel.java:2404)                                                在android.os.Parcel.readParcelable(Parcel.java:2358)                                                在android.os.Parcel.readValue(Parcel.java:2264)                                                在android.os.Parcel.readArrayMapInternal(Parcel.java:2614)                                                在android.os.BaseBundle.unparcel(BaseBundle.java:221)                                                在android.os.BaseBundle.getString(BaseBundle.java:920)                                                在android.content.Intent.getStringExtra(Intent.java:6183)                                                在com.android.server.am.ActivityStackSupervisor.startActivityLocked(ActivityStackSupervisor.java:2707)                                                在com.android.server.am.ActivityStackSupervisor.startActivityMayWait(ActivityStackSupervisor.java:2169)                                                在com.android.server.am.ActivityManagerService.startActivityAsUser(ActivityManagerService.java:6322)                                                在com.android.server.am.ActivityManagerService.startActivity(ActivityManagerService.java:6100)                                                在android.app.ActivityManagerNative.onTransact(ActivityManagerNative.java:170)                                                在com.android.server.am.ActivityManagerService.onTransact(ActivityManagerService.java:3990)                                                在android.os.Binder.execTransact(Binder.java:453)                                             引起:java.lang.ClassNotFoundException:com.xxx.model.PhotosList                                                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:469)                                                at java.lang.Class.classForName(Native Method)                                                at java.lang.Class.forName(Class.java:324)                                                在android.os.Parcel.readParcelableCreator(Parcel.java:2404)                                                在android.os.Parcel.readParcelable(Parcel.java:2358)                                                在android.os.Parcel.readValue(Parcel.java:2264)                                                在android.os.Parcel.readArrayMapInternal(Parcel.java:2614)                                                在android.os.BaseBundle.unparcel(BaseBundle.java:221)                                                在android.os.BaseBundle.getString(BaseBundle.java:920)                                                在android.content.Intent.getStringExtra(Intent.java:6183)                                                在com.android.server.am.ActivityStackSupervisor.startActivityLocked(ActivityStackSupervisor.java:2707)                                                在com.android.server.am.ActivityStackSupervisor.startActivityMayWait(ActivityStackSupervisor.java:2169)                                                在com.android.server.am.ActivityManagerService.startActivityAsUser(ActivityManagerService.java:6322)                                                在com.android.server.am.ActivityManagerService.startActivity(ActivityManagerService.java:6100)                                                在android.app.ActivityManagerNative.onTransact(ActivityManagerNative.java:170)                                                在com.android.server.am.ActivityManagerService.onTransact(ActivityManagerService.java:3990)                                                在android.os.Binder.execTransact(Binder.java:453)                                             引起:java.lang.NoClassDefFoundError:使用引导类加载器找不到类;没有可用的堆栈跟踪

我像这样回复了光刻画对象:

getIntent().setExtrasClassLoader(PhotosList.class.getClassLoader());
        PhotosList photosList = bundle.getParcelable(BUNDLE_KEY_PHOTOS);

1 个答案:

答案 0 :(得分:0)

你应该参考这篇文章,从#34; Parcelable,Speed King&#34;开始阅读。您缺少describeContents()和内部类声明Parcelable.Creator<ParcelableDeveloper> CREATOR