0

I want to pass Collection[] Array trough an intentExtra but it won't work.

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

In the other activity:

Intent intent = getIntent();
    Parcelable[] parcelables = intent.getParcelableArrayExtra("ASD");
    mCollections = Arrays.copyOf(parcelables, parcelables.length, Collection[].class);

If it gets to mCollections = Arrays.copyOf(...) line it gets a NullPointerException because the parcelables.length is null...

Here is Collection class:

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

}

Marci
  • 3
  • 1
  • 4
  • answered in [this](http://stackoverflow.com/questions/5460222/pass-an-array-of-custom-objects-android)question and [this](http://stackoverflow.com/questions/13778485/how-do-i-send-an-array-of-objects-from-one-activity-to-another) – Raafat Alhmidi Sep 24 '16 at 14:05
  • Possible duplicate of [How to send an object from one Android Activity to another using Intents?](http://stackoverflow.com/questions/2139134/how-to-send-an-object-from-one-android-activity-to-another-using-intents) – theduck Sep 24 '16 at 14:11

2 Answers2

0

Well, you add the extra using the key "ARRAY_EXTRA", put you try to extract it in the other Activity using the key "ASD". That won't work.

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

In the other activity:

    Intent intent = getIntent();
    Parcelable[] parcelables = intent.getParcelableArrayExtra("ASD");
David Wasser
  • 93,459
  • 16
  • 209
  • 274
0

You should use the putParcelableArrayListExtra() method on the Intent class. Because your are using getParcelableArrayListExtra() method in receiver activity

Jayanth
  • 5,954
  • 3
  • 21
  • 38