我正在尝试将一个parcelable对象的列表写入一个parcelable对象,但是我收到一个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
据我所知,在将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);
}
}
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);
}
}
答案 0 :(得分:4)
试试此代码
public class EventHolder implements Parcelable { public List mEMList = new ArrayList(); public Event mEvent; 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 mEMList, Event mEvent) { this.mEMList = mEMList; this.mEvent = mEvent; } public EventHolder(Parcel in) { in.readList(mEMList, mEMList.getClass().getClassLoader()); this.mEvent = in.readParcelable(mEvent.getClass().getClassLoader()); } public int describeContents() { return 0XFE; } public void writeToParcel(Parcel dest, int flags) { dest.writeList(mEMList); dest.writeParcelable(mEvent, describeContents()); } }
我有一个疑问,即赛事是否具有可分辨性? 如果不能使它成为可以分配的。
答案 1 :(得分: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);
}
}