RuntimeException: Parcel android.os.Parcel: Unmarshalling unknown type code

一、报错内容

E/JavaBinder( 1465): java.lang.RuntimeException: Parcel android.os.Parcel@42571140: Unmarshalling unknown type code 7471207 at offset 400
E/JavaBinder( 1465):    at android.os.Parcel.readValue(Parcel.java:2080)
E/JavaBinder( 1465):    at android.os.Parcel.readListInternal(Parcel.java:2343)
E/JavaBinder( 1465):    at android.os.Parcel.readList(Parcel.java:1578)
E/JavaBinder( 1465):    at com.grandstream.cmcc.contacts.vo.HttpOnePage.readFromParcel(HttpOnePage.java:115)
E/JavaBinder( 1465):    at com.grandstream.cmcc.contacts.vo.HttpOnePage.<init>(HttpOnePage.java:86)
E/JavaBinder( 1465):    at com.grandstream.cmcc.contacts.vo.HttpOnePage$1.createFromParcel(HttpOnePage.java:121)
E/JavaBinder( 1465):    at com.grandstream.cmcc.contacts.vo.HttpOnePage$1.createFromParcel(HttpOnePage.java:118)
E/JavaBinder( 1465):    at android.os.Parcel.readParcelable(Parcel.java:2104)
E/JavaBinder( 1465):    at android.os.Parcel.readValue(Parcel.java:2013)
E/JavaBinder( 1465):    at com.grandstream.convergentconference.entity.Result.readFromParcel(Result.java:68)
E/JavaBinder( 1465):    at com.grandstream.convergentconference.entity.Result.<init>(Result.java:35)
E/JavaBinder( 1465):    at com.grandstream.convergentconference.entity.Result$1.createFromParcel(Result.java:15)
E/JavaBinder( 1465):    at com.grandstream.convergentconference.entity.Result$1.createFromParcel(Result.java:12)
E/JavaBinder( 1465):    at com.grandstream.convergentconference.callback.ITaskCallback$Stub.onTransact(ITaskCallback.java:57)
E/JavaBinder( 1465):    at android.os.Binder.execTransact(Binder.java:404)

二、原因

原因1:readFromParcel()和writeToParcel()对变量的读写顺序必须保持一致
569
570
原因2:若变量是List类型,必须初始化,不能为null
571

三、正确的代码示例

import android.os.Parcel;
import android.os.Parcelable;

import java.util.ArrayList;
import java.util.List;

public class UserBusiness implements Parcelable {

    private String notMobile;
    private String mobile;
    private List<Integer> authority;
    private List<BusinessType> businessTypes = new ArrayList<BusinessType>();
    private int acceptType;

    public String getNotMobile() {
        return notMobile;
    }

    public void setNotMobile(String notMobile) {
        this.notMobile = notMobile;
    }

    public String getMobile() {
        return mobile;
    }

    public void setMobile(String mobile) {
        this.mobile = mobile;
    }

    public List<Integer> getAuthority() {
        return authority;
    }

    public void setAuthority(List<Integer> authority) {
        this.authority = authority;
    }

    public List<BusinessType> getBusinessTypes() {
        return businessTypes;
    }

    public void setBusinessTypes(List<BusinessType> businessTypes) {
        this.businessTypes = businessTypes;
    }

    public int getAcceptType() {
        return acceptType;
    }

    public void setAcceptType(int acceptType) {
        this.acceptType = acceptType;
    }

    public static class Builder{
        UserBusiness userBusiness;
        public Builder(){
            userBusiness = new UserBusiness();
        }
        public Builder setNotMobile(String notMobile){
            userBusiness.notMobile = notMobile;
            return this;
        }

        public Builder setMobile(String mobile){
            userBusiness.mobile = mobile;
            return this;
        }

        public Builder setAuthority(List<Integer> authority){
            userBusiness.authority = authority;
            return this;
        }

        public Builder setBusinessTypes(List<BusinessType> businessTypes){
            userBusiness.businessTypes = businessTypes;
            return this;
        }

        public Builder setAcceptType(int acceptType){
            userBusiness.acceptType = acceptType;
            return this;
        }

        public UserBusiness build(){
            return userBusiness;
        }
    }

    public UserBusiness(){

    }

    public UserBusiness(Parcel source){
        readFromParcel(source);
    }

    private void readFromParcel(Parcel in) {
        this.notMobile = in.readString();
        this.mobile = in.readString();
        this.authority = new ArrayList<Integer>();
        in.readList(authority,  Integer.class.getClassLoader());
        in.readList(businessTypes, BusinessType.class.getClassLoader());
        this.acceptType = in.readInt();
    }

    @Override
    public String toString() {
        return "ContactNode{" +
                "notMobile=" + notMobile +'\'' +
                "mobile=" + mobile +'\'' +
                "businessTypes=" + businessTypes +'\'' +
                "authority=" + authority + '\'' +
                "acceptType=" + acceptType +
                '}';
    }

    public static final Creator<UserBusiness> CREATOR = new Creator<UserBusiness>() {
        @Override
        public UserBusiness createFromParcel(Parcel source) {
            return new UserBusiness(source);
        }

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

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

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(notMobile);
        dest.writeString(mobile);
        dest.writeList(authority);
        dest.writeList(businessTypes);
        dest.writeInt(acceptType);
    }


}
这就是使用Parcelable时遇到的坑,后面会继续总结
0

留下评论