2

我正在尝试将可打包对象的列表写入可打包对象,但我收到一个 java 运行时错误,提示“解组时找不到类...”。

日志猫

11-13 08:41:06.412: E/Parcel(791): Class not found when unmarshalling: com.firstandroidapp.EventMeta
11-13 08:41:06.412: E/Parcel(791): java.lang.ClassNotFoundException: com.firstandroidapp.EventMeta
11-13 08:41:06.412: E/Parcel(791):  at java.lang.Class.classForName(Native Method)
11-13 08:41:06.412: E/Parcel(791):  at java.lang.Class.forName(Class.java:204)
11-13 08:41:06.412: E/Parcel(791):  at java.lang.Class.forName(Class.java:169)
11-13 08:41:06.412: E/Parcel(791):  at android.os.Parcel.readParcelableCreator(Parcel.java:2091)
11-13 08:41:06.412: E/Parcel(791):  at android.os.Parcel.readParcelable(Parcel.java:2055)
11-13 08:41:06.412: E/Parcel(791):  at android.os.Parcel.readValue(Parcel.java:1971)
11-13 08:41:06.412: E/Parcel(791):  at android.os.Parcel.readListInternal(Parcel.java:2264)
11-13 08:41:06.412: E/Parcel(791):  at android.os.Parcel.readList(Parcel.java:1537)
11-13 08:41:06.412: E/Parcel(791):  at com.firstandroidapp.EventHolder.<init>(EventHolder.java:36)
11-13 08:41:06.412: E/Parcel(791):  at com.firstandroidapp.EventHolder$1.createFromParcel(EventHolder.java:17)
11-13 08:41:06.412: E/Parcel(791):  at com.firstandroidapp.EventHolder$1.createFromParcel(EventHolder.java:1)
11-13 08:41:06.412: E/Parcel(791):  at android.os.Parcel.readParcelable(Parcel.java:2062)
11-13 08:41:06.412: E/Parcel(791):  at android.os.Parcel.readValue(Parcel.java:1971)
11-13 08:41:06.412: E/Parcel(791):  at android.os.Parcel.readMapInternal(Parcel.java:2255)
11-13 08:41:06.412: E/Parcel(791):  at android.os.Bundle.unparcel(Bundle.java:223)
11-13 08:41:06.412: E/Parcel(791):  at android.os.Bundle.getParcelable(Bundle.java:1170)
11-13 08:41:06.412: E/Parcel(791):  at android.content.Intent.getParcelableExtra(Intent.java:4501)
11-13 08:41:06.412: E/Parcel(791):  at com.firstandroidapp.AddSubjectFragment.onActivityResult(AddSubjectFragment.java:198)
11-13 08:41:06.412: E/Parcel(791):  at android.support.v4.app.FragmentActivity.onActivityResult(FragmentActivity.java:166)
11-13 08:41:06.412: E/Parcel(791):  at android.app.Activity.dispatchActivityResult(Activity.java:5322)
11-13 08:41:06.412: E/Parcel(791):  at android.app.ActivityThread.deliverResults(ActivityThread.java:3363)
11-13 08:41:06.412: E/Parcel(791):  at android.app.ActivityThread.handleSendResult(ActivityThread.java:3410)
11-13 08:41:06.412: E/Parcel(791):  at android.app.ActivityThread.access$1100(ActivityThread.java:141)
11-13 08:41:06.412: E/Parcel(791):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1304)
11-13 08:41:06.412: E/Parcel(791):  at android.os.Handler.dispatchMessage(Handler.java:99)
11-13 08:41:06.412: E/Parcel(791):  at android.os.Looper.loop(Looper.java:137)
11-13 08:41:06.412: E/Parcel(791):  at android.app.ActivityThread.main(ActivityThread.java:5103)
11-13 08:41:06.412: E/Parcel(791):  at java.lang.reflect.Method.invokeNative(Native Method)
11-13 08:41:06.412: E/Parcel(791):  at java.lang.reflect.Method.invoke(Method.java:525)
11-13 08:41:06.412: E/Parcel(791):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
11-13 08:41:06.412: E/Parcel(791):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
11-13 08:41:06.412: E/Parcel(791):  at dalvik.system.NativeStart.main(Native Method)
11-13 08:41:06.412: E/Parcel(791): Caused by: java.lang.NoClassDefFoundError: com/firstandroidapp/EventMeta
11-13 08:41:06.412: E/Parcel(791):  ... 32 more
11-13 08:41:06.412: E/Parcel(791): Caused by: java.lang.ClassNotFoundException: Didn't find class "com.firstandroidapp.EventMeta" on path: DexPathList[[directory "."],nativeLibraryDirectories=[/system/lib]]
11-13 08:41:06.412: E/Parcel(791):  at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:53)
11-13 08:41:06.412: E/Parcel(791):  at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
11-13 08:41:06.412: E/Parcel(791):  at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
11-13 08:41:06.412: E/Parcel(791):  ... 32 more

据我了解,在编写 to 列表时会发生EventMeta错误EventHolder

以下是 和 的EventHolder实现EventMeta

事件持有者

public class EventHolder implements Parcelable {

    public List<EventMeta> mEMList = new ArrayList<EventMeta>();
    public Event mEvent;

    @SuppressWarnings("rawtypes")
    public static final Parcelable.Creator CREATOR = new Parcelable.Creator() {
        public EventHolder createFromParcel(Parcel in) {
            return new EventHolder(in); 
        }

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


    public EventHolder(List<EventMeta> mEMList, Event mEvent) {
        this.mEMList = mEMList;
        this.mEvent = mEvent;
    }

    public EventHolder(Parcel in){
        mEMList= new ArrayList<EventMeta>();
        in.readTypedList(mEMList, EventMeta.CREATOR);
        this.mEvent = in.readParcelable(null);
    }

    public int describeContents(){
        return 0;
    }

    public void writeToParcel(Parcel dest, int flags) {
        dest.writeTypedList(mEMList); 
        dest.writeParcelable(mEvent, flags);
    }
}

事件元

public class EventMeta implements Parcelable {

    long mEventMetaId;
    long mEventId;
    Date mStartDate;

    @SuppressWarnings("rawtypes")
    public static final Parcelable.Creator CREATOR = new Parcelable.Creator() {
        public EventMeta createFromParcel(Parcel in) {
            return new EventMeta(in);
        }

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

    public EventMeta(long mEventMetaId, long mEventId, Date mStartDate) {
        this.mEventMetaId = mEventMetaId;
        this.mEventId = mEventId;
        this.mStartDate = mStartDate;
    }

    public EventMeta(Parcel in) {
        this.mEventMetaId = in.readLong();
        this.mEventId = in.readLong();
        this.mStartDate = new Date(in.readLong());
    }

    public int describeContents() {
        return 0;
    }

    public void writeToParcel(Parcel dest, int flags) {
        dest.writeLong(this.mEventMetaId);
        dest.writeLong(this.mEventId);
        dest.writeLong(this.mStartDate.getTime());
    }

}

我的代码有什么问题吗?我在发布之前仔细检查了任何内容,但找不到任何不合适的地方。

更新 ##

我使用了 daneejela 的答案,现在问题已转移到名为“Class”的类 - 这意味着问题已部分解决。

Class扩展`事件,它看起来像这样:

public class Class extends Event {  

    long mClassId;
    String mClassLocation;
    long mSubjectId;

    @SuppressWarnings("rawtypes")
    public static final Parcelable.Creator CREATOR = new Parcelable.Creator() {
        public Class createFromParcel(Parcel in) {
            return new Class(in);
        }

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

    public Class(long mClassId, String location, long mSubjectId, long mEventId, int mEventPriority, String mEventTitle, String mEventNotes, long start, long end) {
        super(mEventId, mEventPriority, mEventTitle, mEventNotes, start, end);
        this.mClassId = mClassId;
        this.mClassLocation = location;
        this.mSubjectId = mSubjectId;
    }

    public Class(Parcel in){
        super(in);
        this.mClassId = in.readLong();
        this.mClassLocation = in.readString();
        this.mSubjectId = in.readLong();
    }

    public int describeContents(){
        return 0;
    }

    public void writeToParcel(Parcel dest, int flags) {
        super.writeToParcel(dest, flags);
        dest.writeLong(this.mClassId);
        dest.writeString(this.mClassLocation);
        dest.writeLong(this.mSubjectId);
    }
}

更新 2

事件

public class Event implements Parcelable {

    long mEventId;
    int mEventPriority;
    String mEventTitle;
    String mEventNotes;
    long mEventStartTime;
    long mEventEndTime;

    @SuppressWarnings("rawtypes")
    public static final Parcelable.Creator CREATOR = new Parcelable.Creator() {
        public Event createFromParcel(Parcel in) {
            return new Event(in); 
        }

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

    public Event(long id, int priority, String title, String notes, long start, long end) {
        this.mEventId = id;
        this.mEventPriority = priority;
        this.mEventTitle = title;
        this.mEventNotes = notes;
        this.mEventStartTime = start;
        this.mEventEndTime = end;
    }

    public Event(Parcel in){
        this.mEventId = in.readLong();
        this.mEventPriority = in.readInt();
        this.mEventTitle = in.readString();
        this.mEventNotes = in.readString();
        this.mEventStartTime = in.readLong();
        this.mEventEndTime = in.readLong();
    }

    public int describeContents(){
        return 0;
    }

    public void writeToParcel(Parcel dest, int flags) {
        dest.writeLong(this.mEventId);
        dest.writeInt(this.mEventPriority);
        dest.writeString(this.mEventTitle);
        dest.writeString(this.mEventNotes);
        dest.writeLong(this.mEventStartTime);
        dest.writeLong(this.mEventEndTime);
    }
}
4

2 回答 2

4

试试这个代码

公共类 EventHolder 实现 Parcelable {
    公共列表 mEMList = new ArrayList();
    公共事件 mEvent;

    公共静态最终 Parcelable.Creator CREATOR = new Parcelable.Creator() {
        public EventHolder createFromParcel(Parcel in) {
            返回新的 EventHolder(in);
        }

        公共 EventHolder[] newArray(int size) {
            返回新的EventHolder[大小];
        }
    };

    公共EventHolder(列表mEMList,事件mEvent){
        this.mEMList = meMList;
        这个.mEvent = mEvent;
    }

    公共EventHolder(包裹){
        in.readList(mEMList, mEMList.getClass().getClassLoader());
        this.mEvent = in.readParcelable(mEvent.getClass().getClassLoader());
    }

    公共 int describeContents() {
        返回 0XFE;
    }

    公共无效writeToParcel(包裹目的地,int标志){
        dest.writeList(mEMList);
        dest.writeParcelable(mEvent, describeContents());
    }
}

我有一个疑问是 Event 是 parcelable 吗?如果不使它成为可包裹的。

于 2013-11-13T14:00:35.727 回答
3

你试过这个(在EventHolder里面):

public class EventHolder implements Parcelable {

//...

public EventHolder(Parcel in){
    mEMList= new ArrayList<EventMeta>();
    in.readTypedList(mEMList, EventMeta.CREATOR);
    this.mEvent = in.readParcelable(null);
}

public int describeContents(){
    return 0;
}

public void writeToParcel(Parcel dest, int flags) {
    dest.writeTypedList(mEMList); 
    dest.writeParcelable(mEvent, flags);
}

}
于 2013-11-13T14:16:48.717 回答