已修正:在底部回答
我正在为一个项目编写一个android应用程序,而我正在尝试使用可打包对象。
我有2个可分解的类。第一个是只有两个参数的普通类。
第二个是具有附加属性的类,以及具有第一个类的类型及其附加Object的列表。
每次我尝试将第二个类的对象传递给另一个Activity时,解码时都会收到android.os.BadParcelableException:ClassNotFoundException
然后该应用程序关闭。
我搜索了几个小时,然后尝试了多种解决方案,但没有任何帮助。
当我仅将第一类的对象传递给另一个 Activity 时,错误不会出现。
这是我的代码:
package de.softwareproject.v3.testclasses;
import android.os.Parcel;
import android.os.Parcelable;
public class Cart implements Parcelable {
private String name;
private Integer priceInCents;
public Cart() {}
public Cart(Parcel in){
readFromParcel(in);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getPriceInCents() {
return priceInCents;
}
public void setPriceInCents(Integer priceInCents) {
this.priceInCents = priceInCents;
}
public static final Parcelable.Creator<Cart> CREATOR = new Parcelable.Creator<Cart>() {
@Override
public Cart createFromParcel(Parcel source) {
return new Cart(source);
}
@Override
public Cart[] newArray(int size) {
return new Cart[size];
}
};
@Override
public int describeContents() {
// TODO Auto-generated method stub
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(name);
dest.writeInt(priceInCents);
}
private void readFromParcel(Parcel in) {
name = in.readString();
priceInCents = in.readInt();
}
}
我的第二堂课:
package de.softwareproject.v3.testclasses;
import java.util.ArrayList;
import java.util.List;
import android.os.Parcel;
import android.os.Parcelable;
public class CurrentUser implements Parcelable {
private String username;
private String eMail;
private String password;
private List<Cart> carts;
private Cart activeCart;
public CurrentUser() {}
public CurrentUser(Parcel in) {
readFromParcel(in);
}
private void readFromParcel(Parcel in) {
username = in.readString();
eMail = in.readString();
password = in.readString();
activeCart = in.readParcelable(de.softwareproject.v3.testclasses.Cart.class.getClassLoader());
if (carts == null){ carts = new ArrayList<Cart>(); }
in.readTypedList(carts, Cart.CREATOR);
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String geteMail() {
return eMail;
}
public void seteMail(String eMail) {
this.eMail = eMail;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public List<Cart> getCarts() {
return carts;
}
public void setCarts(List<Cart> carts) {
this.carts = carts;
}
public Cart getActiveCart() {
return activeCart;
}
public void setActiveCart(Cart activeCart) {
this.activeCart = activeCart;
}
@Override
public int describeContents() {
// TODO Auto-generated method stub
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(username);
dest.writeString(eMail);
dest.writeString(password);
dest.writeTypedList(carts);
dest.writeParcelable(activeCart, flags);
}
public static final Parcelable.Creator<CurrentUser> CREATOR = new Parcelable.Creator<CurrentUser>() {
@Override
public CurrentUser createFromParcel(Parcel in) {
return new CurrentUser(in);
}
@Override
public CurrentUser[] newArray(int size) {
return new CurrentUser[size];
}
};
}
请帮我。
编辑:这是我的Logcat
03-15 16:24:08.139: E/Parcel(689): Class not found when unmarshalling: ??, e: java.lang.ClassNotFoundException: ??
03-15 16:24:08.149: D/AndroidRuntime(689): Shutting down VM
03-15 16:24:08.149: W/dalvikvm(689): threadid=1: thread exiting with uncaught exception (group=0x409961f8)
03-15 16:24:08.229: E/AndroidRuntime(689): FATAL EXCEPTION: main
03-15 16:24:08.229: E/AndroidRuntime(689): java.lang.RuntimeException: Unable to start activity ComponentInfo{de.softwareproject.v3/de.softwareproject.v3.Startpage}: android.os.BadParcelableException: ClassNotFoundException when unmarshalling: ??
03-15 16:24:08.229: E/AndroidRuntime(689): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1955)
03-15 16:24:08.229: E/AndroidRuntime(689): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
03-15 16:24:08.229: E/AndroidRuntime(689): at android.app.ActivityThread.access$600(ActivityThread.java:122)
03-15 16:24:08.229: E/AndroidRuntime(689): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
03-15 16:24:08.229: E/AndroidRuntime(689): at android.os.Handler.dispatchMessage(Handler.java:99)
03-15 16:24:08.229: E/AndroidRuntime(689): at android.os.Looper.loop(Looper.java:137)
03-15 16:24:08.229: E/AndroidRuntime(689): at android.app.ActivityThread.main(ActivityThread.java:4340)
03-15 16:24:08.229: E/AndroidRuntime(689): at java.lang.reflect.Method.invokeNative(Native Method)
03-15 16:24:08.229: E/AndroidRuntime(689): at java.lang.reflect.Method.invoke(Method.java:511)
03-15 16:24:08.229: E/AndroidRuntime(689): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
03-15 16:24:08.229: E/AndroidRuntime(689): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
03-15 16:24:08.229: E/AndroidRuntime(689): at dalvik.system.NativeStart.main(Native Method)
03-15 16:24:08.229: E/AndroidRuntime(689): Caused by: android.os.BadParcelableException: ClassNotFoundException when unmarshalling: ??
03-15 16:24:08.229: E/AndroidRuntime(689): at android.os.Parcel.readParcelable(Parcel.java:1966)
03-15 16:24:08.229: E/AndroidRuntime(689): at de.softwareproject.v3.testclasses.CurrentUser.readFromParcel(CurrentUser.java:26)
03-15 16:24:08.229: E/AndroidRuntime(689): at de.softwareproject.v3.testclasses.CurrentUser.<init>(CurrentUser.java:19)
03-15 16:24:08.229: E/AndroidRuntime(689): at de.softwareproject.v3.testclasses.CurrentUser$1.createFromParcel(CurrentUser.java:80)
03-15 16:24:08.229: E/AndroidRuntime(689): at de.softwareproject.v3.testclasses.CurrentUser$1.createFromParcel(CurrentUser.java:1)
03-15 16:24:08.229: E/AndroidRuntime(689): at android.os.Parcel.readParcelable(Parcel.java:1992)
03-15 16:24:08.229: E/AndroidRuntime(689): at android.os.Parcel.readValue(Parcel.java:1854)
03-15 16:24:08.229: E/AndroidRuntime(689): at android.os.Parcel.readMapInternal(Parcel.java:2094)
03-15 16:24:08.229: E/AndroidRuntime(689): at android.os.Bundle.unparcel(Bundle.java:223)
03-15 16:24:08.229: E/AndroidRuntime(689): at android.os.Bundle.getParcelable(Bundle.java:1158)
03-15 16:24:08.229: E/AndroidRuntime(689): at de.softwareproject.v3.Startpage.onCreate(Startpage.java:34)
03-15 16:24:08.229: E/AndroidRuntime(689): at android.app.Activity.performCreate(Activity.java:4465)
03-15 16:24:08.229: E/AndroidRuntime(689): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
03-15 16:24:08.229: E/AndroidRuntime(689): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1919)
03-15 16:24:08.229: E/AndroidRuntime(689): ... 11 more
03-15 16:24:10.289: I/Process(689): Sending signal. PID: 689 SIG: 9
FIX
我终于找到了错误,我简直无法相信它!!!
搜索了将近1 1/2天,所需要做的只是切换readFromParcel或writeToParcel中的最后两个方法,因为它们的顺序不完全相同!
简直不敢相信,但现在可以了。
对于每个犯同样错误的人,请检查writeToParcel和readFromParcel的顺序。
Thay必须处于相同的顺序。
祝你有美好的一天,克里斯汀**
最佳答案
FIX我终于找到了错误,我简直无法相信它!!!
搜索了将近1 1/2天,所需要做的只是切换readFromParcel或writeToParcel中的最后两个方法,因为它们的顺序不完全相同!
简直不敢相信,但现在可以了。对于每个犯同样错误的人,请检查writeToParcel和readFromParcel的顺序。 Thay必须处于相同的顺序。
(克里斯蒂安·里施(Christian Risch)自己解决了这个问题。不过,我相信这个网站的整洁。也许他会自己回答。