用parcelable解组异常

时间:2013-03-31 17:31:15

标签: android parcelable

我尝试使用整数的arraylist传递一个parcelable对象但是当android尝试读取parcelable对象时出现错误

 03-31 19:20:12.484: E/AndroidRuntime(21448): Caused by: java.lang.RuntimeException: Parcel android.os.Parcel@4172c410: Unmarshalling unknown type code 7602291 at offset 192
03-31 19:20:12.484: E/AndroidRuntime(21448):    at android.os.Parcel.readValue(Parcel.java:2032)
03-31 19:20:12.484: E/AndroidRuntime(21448):    at android.os.Parcel.readListInternal(Parcel.java:2235)
03-31 19:20:12.484: E/AndroidRuntime(21448):    at android.os.Parcel.readList(Parcel.java:1531)

这是我的可分配对象

public class Country implements Parcelable {

    private String name;
    private int id;
    private List<Integer> listTest;


     public Country () {
            super();
            listTest= new ArrayList<Integer>();
        }


    public Country (String name, int id, List <Integer> listTest) {
        super();
        this.name= name;
        this.id=id;
        this.listTest= listTest;
    }

public Country (Parcel in) {

        this.name= in.readString();
        this.id= in.readInt();
        in.readList(listTest,Integer.class.getClassLoader());//error here

    }

    //getters,setters

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

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


@Override
public int describeContents() {
    // TODO Auto-generated method stub
    return 0;
}

@Override
public void writeToParcel(Parcel dest, int flags) {
    dest.writeString(name);
    dest.writeInt(id);
    dest.writeList(listTest);


}

}

我检查了我的列表,它只包含整数,所以我不知道这个错误来自哪里

非常感谢您的帮助

1 个答案:

答案 0 :(得分:2)

我改变了

in.readList(listTest,List.class.getClassLoader());  

我没有错误。