readParcelable返回null

时间:2016-05-18 15:00:13

标签: android parcelable

我尝试将News类传递给一个活动,因此我实现了Parcelable接口。在News内,我有两个实现Parcelable的课程,ImageDate

问题是我的News对象末尾包含null字段imagedate

这是我的代码:

News.java

public class News implements Parcelable {
public static final String TAG = "model_news";

private JSONObject object;

private int id;
private String type;
private String title;
private Boolean comment_disabled;
private String category_name;
private String url;
private Image images;
private Date date;
private Boolean is_video;

public News(JSONObject object) {
    this.object = object;

    try {
        id = Integer.parseInt(object.getString("id"));
        type = object.getString("type");
        title = object.getString("title");
        comment_disabled = object.getBoolean("comment_disabled");
        category_name = object.getString("category_name");
        url = object.getString("url");

        if (!object.isNull("images")) {
            images = new Image(object.getJSONObject("images"));
        }

        date = new Date(object.getJSONObject("date"));

        is_video = object.getBoolean("is_video");

    } catch (JSONException e) {
        Log.e(TAG, e.getMessage());
    }

}

protected News(Parcel in) {
    id = in.readInt();
    type = in.readString();
    title = in.readString();
    category_name = in.readString();
    url = in.readString();
    images =  (Image) in.readParcelable(Image.class.getClassLoader());
    date = (Date) in.readParcelable(Date.class.getClassLoader());
    is_video = in.readByte() != 0;
    comment_disabled = in.readByte() != 0;
}

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

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

@Override
public void writeToParcel(Parcel dest, int flags) {
    dest.writeInt(id);
    dest.writeString(type);
    dest.writeString(title);
    dest.writeByte((byte) (comment_disabled ? 1 : 0));
    dest.writeString(category_name);
    dest.writeString(url);
    dest.writeParcelable(images, flags);
    dest.writeParcelable(date, flags);
    dest.writeByte((byte) (is_video ? 1 : 0));
}

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

}

Image.java

public class Image implements Parcelable {
public static final String TAG = "model_image";

private JSONObject imageObj;
private JSONObject original;

private String source;
private int width;
private Drawable image;

public Image(JSONObject imageObj) {
    this.imageObj = imageObj;

    try {
        original = this.imageObj.getJSONObject("original");
        source = original.getString("src");
        width = original.getInt("width");
    } catch (JSONException e) {
        e.getMessage();
    }
}

protected Image(Parcel in) {
    source = in.readString();
    width = in.readInt();
}

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

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

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

@Override
public void writeToParcel(Parcel dest, int flags) {
    dest.writeString(source);
    dest.writeInt(width);
}

}

我做错了什么?

2 个答案:

答案 0 :(得分:2)

Parcelable实施中存在错误。

首先,parcelable实现声明:在News(Parcel in)构造函数中传递的字段应该在writeToParcel()方法中以相同的顺序编写。那就叫做编组和解组。

<强>更正:

  1. Drawable无法在Parcelable中传递参数。

  2. 新闻Parcelable实施。

  3. 错过了一些领域,仅供您理解。

    public class News implements Parcelable {
    
    public static final String TAG = "model_news";
    
        private JSONObject object;
    
        private int id;
        private String type;
        private String title;
        private Boolean comment_disabled;
        private String category_name;
        private String url;
        private Image images;
        private Date date;
        private Boolean is_video;
    
        protected News(Parcel in) {
            id = in.readInt();
            type = in.readString();
            title = in.readString();
            category_name = in.readString();
            url = in.readString();
        }
    
        public static final Creator<News> CREATOR = new Creator<News>() {
            @Override
            public News createFromParcel(Parcel in) {
                return new News(in);
            }
    
            @Override
            public News[] newArray(int size) {
                return new News[size];
            }
        };
    
        @Override
        public int describeContents() {
            return 0;
        }
    
        @Override
        public void writeToParcel(Parcel dest, int flags) {
            dest.writeInt(id);
            dest.writeString(type);
            dest.writeString(title);
            dest.writeString(category_name);
            dest.writeString(url);
        }
    }
    
    
    
    
    
    
    public class Image implements Parcelable {
        public static final String TAG = "model_image";
    
    private JSONObject imageObj;
    private JSONObject original;
    
    private String source;
    private int width;
    private Drawable image;
    
    protected Image(Parcel in) {
        source = in.readString();
        width = in.readInt();
    }
    
    public static final Creator<Image> CREATOR = new Creator<Image>() {
        @Override
        public Image createFromParcel(Parcel in) {
            return new Image(in);
        }
    
        @Override
        public Image[] newArray(int size) {
            return new Image[size];
        }
    };
    
    @Override
    public int describeContents() {
        return 0;
    }
    
    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(source);
        dest.writeInt(width);
    }
    }
    

答案 1 :(得分:1)

News.writeToParcel中的

dest.writeString(title);
dest.writeByte((byte) (comment_disabled ? 1 : 0));
dest.writeString(category_name);
News.readToParcel中的

title = in.readString();
category_name = in.readString();

我想你忘记了readByte中的readToParcel