-1

I Am trying to send a Parcelable object from an Activity to a IntentSerivce. The code below was working well, I have made changes to the project but not that section, and have no idea why this has stopped working.

Activity:

Bundle bundle = new Bundle();
bundle.putParcelable("client", client);
bundle.putParcelable("job", job);

Intent intent = new Intent(this, EmailService.class);
intent.putExtra("bundle", bundle);
intent.putExtra("type", "type1");

startService(intent);

Service:

protected void onHandleIntent(Intent intent) {
  String type = intent.getStringExtra("type");

  Bundle bundle = intent.getBundleExtra("bundle");
  Client client = bundle.getParcelable("client");
  ...

The line Client client = bundle.getParcelable("client"); causes java.lang.NullPointerException: Attempt to get length of null array. The Client object is not null in the Activity and the bundle is not null in the Service. I am also able to pass my Client object around between activities without any problems.

Why has this stopped working / How do I fix this?

ישו אוהב אותך
  • 28,609
  • 11
  • 78
  • 96
meeeee
  • 2,929
  • 3
  • 22
  • 25

1 Answers1

0

You dont have to put bundle. You can put your object or dto using intent . Then Parse this object in OnCreate of your activity. Thought you need to serialize your object or dto.

Example:

Start activity Like this

Intent intent = new Intent(this, EmailService.class); intent.putExtra("client", client); intent.putExtra("job", job);

Then in Oncreate of you Activity parse like this

Client client = (Client) getIntent().getSerializableExtra("client");
Job job = (Job) getIntent().getSerializableExtra("job");
Wasir
  • 185
  • 2
  • 11