Parcelable的错误用法

这是我在Intent中传Parcelable序列化对象list遇到的错误
先看看这个错误的关键词:unmarshalling和ClassNotFoundException,BadParcelableException

Parcel: Class not found when unmarshalling: 4
    java.lang.ClassNotFoundException: Invalid name
    ···
    .BadParcelableException

找到错误了:implements Parcelable后需要实现的writeToParcel(Parcel dest, int flags) 和构造方法如BaseModel(Parcel in)里面的实现顺序必须一一对应,不然就会出现如上错误。

错误写法:

 @Override
    public void writeToParcel(Parcel dest, int flags) {
        super.writeToParcel(dest, flags);
        dest.writeInt(this.inum);//顺序1
        dest.writeParcelable(this.objA, flags);//顺序2
    }

    protected ABean(Parcel in) {
        super(in);
        this.objA = in.readParcelable(ClassA.class.getClassLoader());//顺序2
        this.inum = in.readInt();//顺序1
    }

这才是正确的写法

@Override
    public void writeToParcel(Parcel dest, int flags) {
        super.writeToParcel(dest, flags);
        dest.writeParcelable(this.objA, flags);//顺序1
        dest.writeInt(this.inum);//顺序2
    }

    protected ABean(Parcel in) {
        super(in);
        this.objA = in.readParcelable(ClassA.class.getClassLoader());//顺序1
        this.inum = in.readInt();//顺序2
    }

这里给个全点的代码(ClassA也是要序列化的,我这就直接继承BaseModel),写法跟Abean一致

  //放入Intent
  Intent intent =new Intent();
  intent.putParcelableArrayListExtra("list",list);
  //取出list
  ArrayList list=getIntent().getParcelableArrayListExtra("list");
import android.annotation.SuppressLint;
import android.os.Parcel;
import android.os.Parcelable;

@SuppressLint("ParcelCreator")
public class BaseModel implements Parcelable {

    @Override
    public int describeContents() { return 0; }

    @Override
    public void writeToParcel(Parcel dest, int flags) {}

    public BaseModel() {}

    protected BaseModel(Parcel in) {}
}
import android.os.Parcel;
import android.os.Parcelable;
public class ABean extends BaseModel {

    private ClassA objA;
    private int intA;
    private String strA;
    private boolean boolA;
    private double doubleA;

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        super.writeToParcel(dest, flags);
        dest.writeInt(this.inum);
        dest.writeParcelable(this.objA, flags);
        dest.writeString(this.code);
        dest.writeByte(this.active ? (byte) 1 : (byte) 0);
        dest.writeDouble(this.retailPrice);
    }

    protected ABean(Parcel in) {
        super(in);
        this.objA = in.readParcelable(ClassA.class.getClassLoader());
        this.inum = in.readInt();
        this.code = in.readString();
        this.active = in.readByte() != 0;
        this.retailPrice = in.readDouble();
    }

    public ABean() {
    }

    public static final Parcelable.Creator CREATOR = new Parcelable.Creator() {
        @Override
        public ABean createFromParcel(Parcel source) {
            return new ABean(source);
        }

        @Override
        public ABean[] newArray(int size) {
            return new ABean[size];
        }
    };

    public ClassA getObjA() {
        return objA;
    }

    public void setObjA(ClassA objA) {
        this.objA = objA;
    }

    public int getIntA() {
        return intA;
    }

    public void setIntA(int intA) {
        this.intA = intA;
    }

    public String getStrA() {
        return strA;
    }

    public void setStrA(String strA) {
        this.strA = strA;
    }

    public boolean isBoolA() {
        return boolA;
    }

    public void setBoolA(boolean boolA) {
        this.boolA = boolA;
    }

    public double getDoubleA() {
        return doubleA;
    }

    public void setDoubleA(double doubleA) {
        this.doubleA = doubleA;
    }
}

你可能感兴趣的:(Parcelable的错误用法)