I have MainActivity and WorkActivity in my app. WorkActivity starting by MainActivity:
Intent intent = new Intent(view.getContext(), WorkActivity.class);
intent.putExtra("intArray", intArray);
startActivity(intent);
In WorkActivity I read intArray param and start foreground service with notification:
protected void onCreate(Bundle savedInstanceState) {
int[] intArray = getIntent().getIntArrayExtra("intArray");
Intent intent = new Intent(this, MyService.class);
intent.putExtra("intArray", intArray);
startService(intent);
}
Service notification:
Intent newIntent = new Intent(this, WorkActivity.class);
PendingIntent pIntent = PendingIntent.getActivity(this, 0, newIntent, 0);
Notification.Builder builder = new Notification.Builder(this)
.setSmallIcon(R.drawable.icon)
.setContentTitle("title)
.setContentIntent(pIntent)
.setContentText("text");
Notification notification;
notification = builder.build();
startForeground(1, notification);
Now, when I close my app and click to notification occur error, because WorkActivity can't get
getIntent().getIntArrayExtra("intArray");
that should bу passed by MainActivity. How to fix this?
Log:
java.lang.RuntimeException: Unable to start service com.test.MyService@9051e18 with Intent { cmp=com.test/.MyService (has extras) }: java.lang.NullPointerException: Attempt to read from null array
Error occur because WorkActivity.getIntent().getIntArrayExtra("intArray") is null. Activity send this to service in lines:
intent.putExtra("intArray", intArray);
startService(intent);
In service I need to get some numbers from intArray like intArray[0], but intArray is null