Android reports Parcelable encountered IOException writing serializable object on Intent transfer value

Today I used Intent to pass values, and I suddenly reported an error "Parcelable encountered IOException writing serializable object", which made me startled. Isn't this a conventional value passing method? The passed value has implemented the serializable interface, and only two basic attributes are defined in the passed object. It is too simple, and an error is reported. Finally, I tried to solve it with my Huashan 72. Here is a record

Error point

Intent qri= new Intent();
 qri.putExtra("hint", "将二维码放入框内,即可自动扫描");
 qri.putExtra("extra", mData);
 qri.setClass(getContext(), QRCodePayActivity.class);
 qri.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
 if (getOwnerActivity() != null) {
     getOwnerActivity().startActivityForResult(qri, requestCode);
 }

The entity of mData is

static class DataBean implements Serializable {
        private String sameDepartureNo;
        private double amount;

        public DataBean(String sameDepartureNo, double amount) {
            this.sameDepartureNo = sameDepartureNo;
            this.amount = amount;
        }

        public String getSameDepartureNo() {
            return sameDepartureNo;
        }

        public void setSameDepartureNo(String sameDepartureNo) {
            this.sameDepartureNo = sameDepartureNo;
        }
 }

Finally resolved

Just complete the get and set methods of the DataBean entity.

to sum up

  1. When the Intent serializes the object by value, the get and set methods either have or none of them. (Easy to miss)
  2. If a member of a serializable class is not a basic type, nor is a String type, then the reference type must also be serializable; otherwise, the class cannot be serialized.

For more serialization knowledge, please refer to https://blog.ixin.run/posts/1480596660

Guess you like

Origin blog.csdn.net/li0978/article/details/113136425