gpt4 book ai didi

java - 查找错误: parcelable BadParcelableException

转载 作者:太空宇宙 更新时间:2023-11-04 12:39:43 27 4
gpt4 key购买 nike

我遇到了著名的 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) 

最佳答案

经过几个小时的工作并尝试了很多事情后,我终于成功修复了它,而且非常简单,属性CREATOR不能拥有其访问器private,我将其更改为public并且它起作用了!

关于java - 查找错误: parcelable BadParcelableException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36939496/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com