嵌套的parcelable对象不读

时间:2013-04-01 14:22:55

标签: android parcelable

我正在尝试包含一个包含一些字符串/ int变量和一个对象变量的对象。字符串和int正在工作,但不是嵌套对象。我明白我也必须这样做,但我显然做错了=。在我的嵌套类中,writeToParcel方法被调用(我使用Log.d()调用进行检查),但createFromParcel()没有。我最终得到一个null对象。这是我的简化代码:

public class MyClass implements Parcelable {

    public MyClass() {
    }

    private Integer id;
    private String name;    
    private MyOtherClass otherClass = new MyOtherClass();

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public OtherClass getOtherClass() {
        return otherClass;
    }

    public void setOtherClass(OtherClass otherClass) {
        this.otherClass = otherClass;
    }

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

        public MyClass[] newArray(int size) {
            return new MyClass[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.writeParcelable(otherClass, flags);
    }

    private MyClass(Parcel in) {
        name = in.readString();
        id = in.readInt();
        otherClass = (OtherClass) in.readParcelable(OtherClass.class.getClassLoader());
    }
}

class MyOtherClass implements Parcelable {

    public OtherClass() {
    }

    private String resourcePreviewURL;

    public String getResourcePreviewURL() {
        return resourcePreviewURL;
    }

    public void setResourcePreviewURL(String resourcePreviewURL) {
        this.resourcePreviewURL = resourcePreviewURL;
    }

    public int describeContents() {
        return 0;
    }

    public void writeToParcel(Parcel out, int flags) {
        Log.d("parcel", "write to parcel"); // this gets called
        out.writeString(resourcePreviewURL);
    }

    public static final Parcelable.Creator<MyOtherClass> CREATOR
            = new Parcelable.Creator<MyOtherClass>() {
        public MyOtherClass createFromParcel(Parcel in) {
            Log.d("parcel", "create from parcel"); // this doesn't get called
            return new MyOtherClass(in);
        }

        public ResourcePreviews[] newArray(int size) {
            return new ResourcePreviews[size];
        }
    };

    private OtherClass(Parcel in) {
            Log.d("parcel", "read from parcel"); // this doesn't get called
        resourcePreviewURL = in.readString();
    }

}

1 个答案:

答案 0 :(得分:67)

我通过改变从包裹写入/读取的顺序解决了这个问题。我将dest.writeParcelable(otherClass, flags);otherClass = (OtherClass) in.readParcelable(OtherClass.class.getClassLoader());调用作为他们方法中的第一个,并开始工作。这是一个问题吗?

@Override
public void writeToParcel(Parcel dest, int flags) {
    dest.writeParcelable(otherClass, flags);
    dest.writeString(name);
    dest.writeInt(id);
}

private MyClass(Parcel in) {
    otherClass = (OtherClass) in.readParcelable(OtherClass.class.getClassLoader());
    name = in.readString();
    id = in.readInt();
}