我正在使用著名的BadParcelableException,但我看不出问题出在哪里,下面是我的Parcelable类:
public class PaymentInfoViewModel implements Parcelable {
private long idEstablishment;
private String nameEstablishment;
private int cardNumber;
private double cardTotalValue;
private String cardTotalValueDesc;
private byte tipPercentage;
public PaymentInfoViewModel() {
}
private static final Parcelable.Creator<PaymentInfoViewModel> CREATOR = new Parcelable.Creator<PaymentInfoViewModel>() {
@Override
public PaymentInfoViewModel createFromParcel(Parcel source) {
return new PaymentInfoViewModel(source);
}
@Override
public PaymentInfoViewModel[] newArray(int size) {
return new PaymentInfoViewModel[size];
}
};
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeLong(idEstablishment);
dest.writeString(nameEstablishment);
dest.writeInt(cardNumber);
dest.writeDouble(cardTotalValue);
dest.writeString(cardTotalValueDesc);
dest.writeByte(tipPercentage);
}
public PaymentInfoViewModel(Parcel source) {
setIdEstablishment(source.readLong());
setNameEstablishment(source.readString());
setCardNumber(source.readInt());
setCardTotalValue(source.readDouble());
setCardTotalValueDesc(source.readString());
setTipPercentage(source.readByte());
}
//getters/setters
}它放在这里,在我的适配器中单击事件:
PaymentInfoViewModel p = new PaymentInfoViewModel();
p.setIdEstablishment(item.getIdEstablishment());
p.setNameEstablishment(item.getName());
p.setTipPercentage((byte)0);
p.setCardTotalValueDesc("");
p.setCardTotalValue(0);
p.setCardNumber(0);
Intent intent = new Intent(context, CardNumberActivity.class);
intent.putExtra(ConstantsUtils.PARAM_INTENT_PAYMENT_INFO, p);
context.startActivity(intent);以及我如何尝试让这个类:
PaymentInfoViewModel paymentInfoViewModel = getIntent().getParcelableExtra(ConstantsUtils.PARAM_INTENT_PAYMENT_INFO);我之前检查过这些链接,但我找不到解决方案:
Parcelable protocol requires a Parcelable.Creator object called CREATOR (I do have CREATOR)
Error While Passing An Object From An Activity To Another (Using Parcelable)
getParcelableExtra:这就是错误(当我尝试编辑的时候):
FATAL EXCEPTION: main
Process: br.com.soutsapp.Souts_v3, PID: 19841
java.lang.RuntimeException: Unable to start activity ComponentInfo{br.com.soutsapp.Souts_v3/br.com.soutsapp.Souts_v3.view.activity.CardNumberActivity}: android.os.BadParcelableException: Parcelable protocol requires a Parcelable.Creator object called CREATOR on class br.com.soutsapp.Souts_v3.model.viewModel.PaymentInfoViewModel
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: android.os.BadParcelableException: Parcelable protocol requires a Parcelable.Creator object called CREATOR on class br.com.soutsapp.Souts_v3.model.viewModel.PaymentInfoViewModel
at android.os.Parcel.readParcelableCreator(Parcel.java:2436)
at android.os.Parcel.readParcelable(Parcel.java:2358)
at android.os.Parcel.readValue(Parcel.java:2264)
at android.os.Parcel.readArrayMapInternal(Parcel.java:2614)
at android.os.BaseBundle.unparcel(BaseBundle.java:221)
at android.os.Bundle.getParcelable(Bundle.java:786)
at android.content.Intent.getParcelableExtra(Intent.java:5377)
at br.com.soutsapp.Souts_v3.view.activity.CardNumberActivity.onCreate(CardNumberActivity.java:30)
at android.app.Activity.performCreate(Activity.java:6251)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 发布于 2016-04-29 21:41:00
经过几个小时的工作和尝试了很多东西,我终于设法修复了它,它真的很简单,属性CREATOR不能有它的访问器private,我把它改为public,它起作用了!
发布于 2016-04-30 00:31:46
如果您还不知道,您可能会对Parceler库感兴趣(尽管您现在已经解决了问题):https://github.com/johncarl81/parceler
(如果我有这样的名声,我会发表评论)
发布于 2021-08-30 06:10:22
在我的例子中是
我只是简单地删除了旧的Parcelable接口代码并实现了新的Parcelable接口代码。
这对我来说很有效..
*编辑
确保您的Parcelable类是静态的。
我的模型类
public class AccountModel implements Parcelable {
private BigDecimal amount;
private int icon;
private String name;
private long id;
private BigDecimal initial;
public AccountModel() {
this.id = new Date().getTime();
this.name = "Untitled";
BigDecimal bigDecimal = BigDecimal.ZERO;
this.amount = bigDecimal;
this.initial = bigDecimal;
this.icon = R.drawable.ic_card;
}
public AccountModel(BigDecimal amount, int icon, String name, long id, BigDecimal initial) {
this.amount = amount;
this.icon = icon;
this.name = name;
this.id = id;
this.initial = initial;
}
public AccountModel( int icon, String name, long id, BigDecimal initial) {
this.icon = icon;
this.name = name;
this.id = id;
this.initial = initial;
}
public AccountModel(long j, String str, BigDecimal bigDecimal, BigDecimal bigDecimal2, int i) {
this.id = j;
this.name = str;
this.amount = bigDecimal;
this.initial = bigDecimal2;
this.icon = i;
}
public AccountModel(BigDecimal initial, String name, int icon)
{
this.initial = initial;
this.name = name;
this.icon = icon;
}
protected AccountModel(Parcel in) {
this.id = in.readLong();
this.name = in.readString();
this.amount = BigDecimal.valueOf(Double.parseDouble(in.readString()));
this.initial = BigDecimal.valueOf(Double.parseDouble(in.readString()));
this.icon = in.readInt();
}
public static final Creator<AccountModel> CREATOR = new Creator<AccountModel>() {
@Override
public AccountModel createFromParcel(Parcel in) {
return new AccountModel(in);
}
@Override
public AccountModel[] newArray(int size) {
return new AccountModel[size];
}
};
@Override
public int describeContents() {
return 0;
}
public static AccountModel newCopyOf(AccountModel account) {
return new AccountModel(account.getId(), account.getName(), account.getAmount(), account.getInitial(), account.getIcon());
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeLong(this.id);
dest.writeString(this.name);
dest.writeString(String.valueOf(this.amount));
dest.writeString(String.valueOf(this.initial));
dest.writeInt(this.icon);
}
public BigDecimal getAmount() {
return amount;
}
public void setAmount(BigDecimal amount) {
this.amount = amount;
}
public int getIcon() {
return icon;
}
public void setIcon(int icon) {
this.icon = icon;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public BigDecimal getInitial() {
return initial;
}
public void setInitial(BigDecimal initial) {
this.initial = initial;
}
}https://stackoverflow.com/questions/36939496
复制相似问题