I want to send a parcelable object to both an activity and a service. For sending to both I use:
public void onClick(View view) {
Intent intent = new Intent(mContext, MainActivity.class);
Intent i = new Intent(mContext, OverlayService.class);
intent.putExtra(ITEM_KEY, item);
i.putExtra(ITEM_KEY, item);
mContext.startActivity(intent);
}
Then to receive, in my activity:
DataItem item = getIntent().getExtras().getParcelable(DataItemAdapter.ITEM_KEY);
if (item == null) {
throw new AssertionError("Null data item received!");
}
This works perfectly. But for my service I use:
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
DataItem item = intent.getExtras().getParcelable(DataItemAdapter.ITEM_KEY);
if (item == null) {
throw new AssertionError("Null data item received!");
}else{
Toast.makeText(this, "Item received", Toast.LENGTH_SHORT).show();
}
return START_STICKY;
}
The result is a nullpointer exception:
Unable to start service com.madhatter.nat.test.OverlayService@7aae5dd with Intent { cmp=com.madhatter.nat.test/.OverlayService }: java.lang.NullPointerException: Attempt to invoke virtual method 'android.os.Parcelable android.os.Bundle.getParcelable(java.lang.String)' on a null object reference
at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3314)
at android.app.ActivityThread.-wrap21(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1565)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.os.Parcelable android.os.Bundle.getParcelable(java.lang.String)' on a null object reference
at com.madhatter.nat.test.OverlayService.onStartCommand(OverlayService.java:46)
at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3297)
at android.app.ActivityThread.-wrap21(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1565)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
Here is the code I use to start the service:
public void show(View view) {
checkPermissionOverlay();
Intent svc = new Intent(this, OverlayService.class);
startService(svc);
notificationService();
}
Any ideas what I'm doing wrong? Thanks for any help.
You need to include the extras in the intent that you use to start the service in the same manner that you are starting the activity.
public void show(View view) {
checkPermissionOverlay();
Intent svc = new Intent(this, OverlayService.class);
svc.putExtra(DataItemAdapter.ITEM_KEY, dataItem);
startService(svc);
notificationService();
}