2016-09-24 79 views
0

我想通過集合[]數組通過intentExtra但它不會工作。爲什麼我不能通過一個意圖傳遞一個對象數組?

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

如果它得到它得到一個NullPointerException,因爲parcelables.length爲空mCollections = Arrays.copyOf(...)線...

這裏是集合類:

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

在[this]中回答(http://stackoverflow.com/questions/5460222/pass-an-array-of-custom-objects-android)問題和[this] (http://stackoverflow.com/questions/13778485/how-do-i-send-an-array-of-objects-from-one-activity-to-another) –

+0

可能的重複[如何發送對象一個Android活動到另一個使用Intents?](http://stackoverflow.com/questions/2139134/how-to-send-an-object-from-one-android-activity-to-another-using-intents) – theduck

回答

0

那麼,你使用鍵「ARRAY_EXTRA」添加額外的,讓你嘗試使用關鍵「ASD」在其他Activity提取它。這是行不通的。

intent.putExtra("ARRAY_EXTRA", jsonData.getCollections()); 
    startActivity(intent); 

在其他活動:

Intent intent = getIntent(); 
    Parcelable[] parcelables = intent.getParcelableArrayExtra("ASD"); 
0

您應該Intent類使用putParcelableArrayListExtra()方法。 因爲您在接收器活動中使用getParcelableArrayListExtra()方法

相關問題