我尝试将News
类传递给一个活动,因此我实现了Parcelable
接口。在News
内,我有两个实现Parcelable
的课程,Image
和Date
问题是我的News
对象末尾包含null
字段image
和date
。
这是我的代码:
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);
}
}
我做错了什么?
答案 0 :(得分:2)
Parcelable实施中存在错误。
首先,parcelable实现声明:在News(Parcel in)构造函数中传递的字段应该在writeToParcel()方法中以相同的顺序编写。那就叫做编组和解组。
<强>更正:强>
Drawable无法在Parcelable中传递参数。
新闻Parcelable实施。
错过了一些领域,仅供您理解。
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
。