8

我正在尝试在我的 parcelable 中传递一个列表:

public class MetaDados implements Parcelable {

private List<Long> sizeImages;

public MetaDados(List<Long> sizeImages){
this.sizeImages = sizeImages;
}

public List<Long> getSizeImages() {
        return sizeImages;
    }

    public void setSizeImages(List<Long> sizeImages) {
        this.sizeImages = sizeImages;
    }

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

@Override
    public void writeToParcel(Parcel dest, int flags) {
dest.writeList(sizeImages);
}

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

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

private MetaDados(Parcel in) {
//HERE IS THE PROBLEM, I'VE tried this:

sizeImages = in.readList(sizeImages, Long.class.getClassLoader());

//but what i got: Type mismatch: cannot convert from void to List<Long>
}
}
4

1 回答 1

18

试试这个:

sizeImages = new ArrayList<Long>(); // or any other type of List
in.readList(sizeImages, null);

Android 文档Parcel.readList说:

从包裹中读入现有的列表对象

因此,您需要先创建List.

于 2013-04-02T21:00:32.857 回答