我想通过intentExtra传递Collection []数组,但它不会工作。
Intent intent = new Intent(SzabadEuMusorokLeirasActivity.this, SzabadEuMusorokViewActivity.class);
JSONdata jsonData = new JSONdata("http://1956.osaarchivum.org/api/items?collection=13");
intent.putExtra("ARRAY_EXTRA", jsonData.getCollections());
startActivity(intent);
在另一项活动中:
Intent intent = getIntent();
Parcelable[] parcelables = intent.getParcelableArrayExtra("ASD");
mCollections = Arrays.copyOf(parcelables, parcelables.length, Collection[].class);
如果它到达mCollections = Arrays.copyOf(...)行,它会得到一个NullPointerException,因为parcelables.length为null ...
这是Collection类:
public class Collection implements Parcelable{
private String mTitile;
private String mSubject;
private String mMediaURL;
public Collection() {}
protected Collection(Parcel in){
mTitile = in.readString();
mSubject = in.readString();
mMediaURL = in.readString();
}
public static final Creator<Collection> CREATOR = new Creator<Collection>() {
@Override
public Collection createFromParcel(Parcel in) {
return new Collection(in);
}
@Override
public Collection[] newArray(int size) {
return new Collection[size];
}
};
public String getTitile() {
return mTitile;
}
public void setTitile(String titile) {
mTitile = titile;
}
public String getSubject() {
return mSubject;
}
public void setSubject(String subject) {
mSubject = subject;
}
public String getMediaURL() {
return mMediaURL;
}
public void setMediaURL(String mediaURL) {
mMediaURL = mediaURL;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(mTitile);
dest.writeString(mSubject);
dest.writeString(mMediaURL);
}
}
答案 0 :(得分:0)
好吧,你使用密钥&#34; ARRAY_EXTRA&#34;添加额外的内容,让你尝试使用密钥&#34; ASD&#34;在另一个Activity
中提取它。那不会起作用。
intent.putExtra("ARRAY_EXTRA", jsonData.getCollections());
startActivity(intent);
在另一项活动中:
Intent intent = getIntent();
Parcelable[] parcelables = intent.getParcelableArrayExtra("ASD");
答案 1 :(得分:0)
您应该在Intent类上使用putParcelableArrayListExtra()
方法。
因为您在接收器活动中使用getParcelableArrayListExtra()
方法