不读书嵌套parcelable对象 [英] Nested parcelable object not reading

查看:193
本文介绍了不读书嵌套parcelable对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想包裹一个对象,它包含了一些字符串/ INT变量和对象变量。琴弦和INT的工作,但不是嵌套的对象。我明白我必须还parcelable,但我显然是做错了=。在我的嵌套类,在 writeToParcel 方法被调用(我检查了 Log.d()通话),但在 createFromParcel()没有。我最终得到一个对象。这是我简单的code:

 公共类MyClass的实现Parcelable {

    公共MyClass的(){
    }

    私人整数ID;
    私人字符串名称;
    私人MyOtherClass otherClass =新MyOtherClass();

    公共整数的getId(){
        返回ID;
    }

    公共无效SETID(整数ID){
        this.id = ID;
    }

    公共字符串的getName(){
        返回名称;
    }

    公共无效setname可以(字符串名称){
        this.name =名称;
    }

    公共OtherClass getOtherClass(){
        返回otherClass;
    }

    公共无效setOtherClass(OtherClass otherClass){
        this.otherClass = otherClass;
    }

    公共静态最终Parcelable.Creator< MyClass的> CREATOR
            =新Parcelable.Creator< MyClass的>(){
        公共MyClass的createFromParcel(包裹中){
            返回新MyClass的(中);
        }

        公共MyClass的[] newArray(INT尺寸){
            返回新MyClass的【尺寸】;
        }
    };

    @覆盖
    公众诠释describeContents(){
        // TODO自动生成方法存根
        返回0;
    }

    @覆盖
    公共无效writeToParcel(包裹DEST,INT标志){
        dest.writeString(名称);
        dest.writeInt(ID);
        dest.writeParcelable(otherClass,旗);
    }

    私人MyClass的(包裹的){
        名称= in.readString();
        的id = in.readInt();
        otherClass =(OtherClass)in.readParcelable(OtherClass.class.getClassLoader());
    }
}

类MyOtherClass实现Parcelable {

    公共OtherClass(){
    }

    私人字符串资源previewURL;

    公共字符串的getResource previewURL(){
        返回资源previewURL;
    }

    公共无效setResource previewURL(字符串资源previewURL){
        this.resource previewURL =资源previewURL;
    }

    公众诠释describeContents(){
        返回0;
    }

    公共无效writeToParcel(包裹出来,诠释标志){
        Log.d(包裹,写包裹); //这个被称为
        out.writeString(资源previewURL);
    }

    公共静态最终Parcelable.Creator< MyOtherClass> CREATOR
            =新Parcelable.Creator< MyOtherClass>(){
        公共MyOtherClass createFromParcel(包裹中){
            Log.d(包裹,从包裹创建); //这个不会被调用
            返回新MyOtherClass(中);
        }

        公共资源previews [] newArray(INT尺寸){
            返回新的资源previews【尺寸】;
        }
    };

    私人OtherClass(包裹中){
            Log.d(包裹,从包裹读); //这个不会被调用
        资源previewURL = in.readString();
    }

}
 

解决方案

我通过更改我写/从包裹中读取的顺序解决了这一点。我做了 dest.writeParcelable(otherClass,旗); otherClass =(OtherClass)in.readParcelable(OtherClass.class.getClassLoader()) ; 要求是先在他们的方法,并开始工作。那是一个问题?

  @覆盖
公共无效writeToParcel(包裹DEST,INT标志){
    dest.writeParcelable(otherClass,旗);
    dest.writeString(名称);
    dest.writeInt(ID);
}

私人MyClass的(包裹的){
    otherClass =(OtherClass)in.readParcelable(OtherClass.class.getClassLoader());
    名称= in.readString();
    的id = in.readInt();
}
 

I'm trying to parcel an object which contains some string/int variables and an object variable. The strings and int's are working, but not the nested object. I understand I would have to it also parcelable, but I'm apparently doing something wrong=. In my nested class, the writeToParcel method gets called (I check with a Log.d() call), but the createFromParcel() doesn't. I end up getting a null object. This is my simplified code:

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();
    }

}

解决方案

I solved this by changing the order in which I write/read from the Parcel. I made the dest.writeParcelable(otherClass, flags); and the otherClass = (OtherClass) in.readParcelable(OtherClass.class.getClassLoader()); calls to be the first in their methods and it started working. Is that an issue?

@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();
}

这篇关于不读书嵌套parcelable对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆