0

I want to create and pass an object through intent to another activity, so I can process the object details in this activity and show it to the user.

This object is being identified by a value long. I have an arraylist (releaseIds) which holds several long values of this object, so I just want to pass these values through an intent to the other activity.

The user can select the desired object from a alertdialog presenting this list, once he/she selects an item from this list the correct long ID is identified and a new object created and passed through intent.

But creating the new object somehow failes, because the object ReleaseModel is null in the receiving acitivity.

What I am doing wrong?

Creating and passing the object (ReleaseModel):

...
new DialogInterface.OnClickListener() {

    public void onClick(DialogInterface dialog, int item) {
    // Do something with the selection
    for (int i = 0; i < releaseIds.size(); i++) {

    if (i == item) {
       long ID = releaseIds.get(i);
       final ReleaseModel releaseModel = new ReleaseModel(ID);
       ReleaseActivtiy_.intent(MyAppsMain.this).extra("releaseModel",
           releaseModel).start();
        }
       }
      }
});
...

The object itself:

public class ReleaseModel extends BaseModel {

    private List<String> url = new ArrayList<>();
    private boolean liked;
    ...

    public ReleaseModel() {
    }

    public ReleaseModel(long id) {
        super(id);
    }
    ...
    public void writeToParcel(Parcel parcel, int flags) {
        super.writeToParcel(parcel, flags);
        parcel.writeInt(liked ? 1 : 0);
        ...
    }

    public static final Parcelable.Creator<ReleaseModel> CREATOR = new Parcelable.Creator<ReleaseModel>() {
        public ReleaseModel createFromParcel(Parcel in) {
            return new ReleaseModel(in);
        }

        public ReleaseModel[] newArray(int size) {
            return new ReleaseModel[size];
        }
    };

    private ReleaseModel(Parcel parcel) {
        super(parcel);
        setLiked(parcel.readInt() == 1);
        ...
    }
}

The basemodel:

public class BaseModel implements Parcelable {

    private long id;
    private long date;
    ...
    public BaseModel() {
    }

    public BaseModel(long id) {
        this.id = id;
    }

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }
    ...
    public void writeToParcel(Parcel parcel, int flags) {
        parcel.writeLong(id);
        parcel.writeLong(date);
        ...
    }

    public static final Parcelable.Creator<BaseModel> CREATOR = new Parcelable.Creator<BaseModel>() {
        public BaseModel createFromParcel(Parcel in) {
            return new BaseModel(in);
        }

        public BaseModel[] newArray(int size) {
            return new BaseModel[size];
        }
    };

    public BaseModel(Parcel parcel) {
        setId(parcel.readLong());
        setDate(parcel.readLong());
        ...
    }
}

Receiving activity:

@EActivity(R.layout.release_activity)
public class ReleaseActivtiy extends BaseActivity implements LoaderManager
        .LoaderCallbacks<BaseResponse>, NotificationCenter.NotificationCenterDelegate {
...
@Extra("releaseModel")
    ReleaseModel releaseModel;

    @Override
        public void onCreate(Bundle savedInstanceState) {

            super.onCreate(savedInstanceState);
            ...
        }

        @AfterViews
        public void init() {
            // Nullpointer exception on this line for releaseModel
            String temp = releaseModel.getSubject().replaceAll("[^a-zA-Z]", "");
        }
}
Simon
  • 1,691
  • 2
  • 13
  • 28

1 Answers1

0

To pass ReleaseModel object to another Activity(ActivityPassedTo). Do the Following Steps:

1.You Need to make ReleaseModel implements Serializable

2.To pass it to an Intent you need to have a Unique Key to identify the object passed in both the activities

Eg. you can create a Key: static final String RELEASE_MODEL = "RELEASE_MODEL";

  1. Now pass it to the activity in intent

    Intent intent = new Intent(this,ActivityPassedTo.class);

    intent.putExtra(RELEASE_MODEL,releaseModelObject);

    startActivity(intent);

  2. Now Retrieve the object in Activity (ActivityPassedTo)

    Intent intent = getIntent()

    ReleaseModel releaseModel = (ReleaseModel)intent.getSerializableExtra(RELEASE_MODEL);

theboringdeveloper
  • 1,429
  • 13
  • 17
  • Just the releasemodel, so I can process it in the receiving activity. The arraylist just holds the long IDs which identifies these objects but creating a new object (releasemodel) fails as it is null in receiving activity. – Simon Dec 26 '17 at 08:39
  • @Simon did that help? – theboringdeveloper Dec 26 '17 at 09:17
  • Hi Gursheesh, I found the error, I forgot to actually retrieve the data which is linked to the object, therefore it was null. It works now but thank you for your efforts, highly appreciated. – Simon Dec 26 '17 at 09:25