I want to make a Notification to control my music player and so I want to start a foreground service.
My Code:
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
protected void onStop(){
super.onStop();
System.out.println("Stop");
Toast.makeText(this,"Stopped",Toast.LENGTH_SHORT).show();
if(play_media.isPlaying()){
PendingIntent pi = PendingIntent.getActivity(getApplicationContext(), 0,
new Intent(getApplicationContext(), MainActivity.class),
PendingIntent.FLAG_UPDATE_CURRENT);
Notification notification = new Notification.Builder(mContext)
.setContentTitle("Playing")
.setContentText("playing a song")
.setSmallIcon(R.drawable.ic_play)
.setLargeIcon(list.get(mDrawerList.getCheckedItemPosition()).getAlbum_Art())
.setContentIntent(pi)
.build();
mediaPlayerService = new MediaPlayerService();
322: mediaPlayerService.startForeground(NOTIFICATION_ID,notification);
}
}
Exception caught on startForeground:
02-12 18:06:08.935 4405-4405/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.Driiinx.musicslide, PID: 4405
java.lang.RuntimeException: Unable to stop activity {...com.Driiinx.musicslide.MainActivity}: java.lang.NullPointerException: class name is null
at android.app.ActivityThread.performStopActivityInner(ActivityThread.java:3188)
...
Caused by: java.lang.NullPointerException: class name is null
at android.content.ComponentName.<init>(ComponentName.java:63)
at android.app.Service.startForeground(Service.java:643)
at com.Driiinx.musicslide.MainActivity.onStop(MainActivity.java:322)
Thanks!
If you have a GameObject for example with a script attached and a variable named rb (rigidbody) this variable will start with null when you start your game. This is why you get a NullReferenceExeption because the computer does not have data stored in that variable.
The rest of this article goes into more detail and shows mistakes that many programmers often make which can lead to a NullReferenceException. The runtime throwing a NullReferenceException always means the same thing: you are trying to use a reference, and the reference is not initialized (or it was once initialized, but is no longer initialized).
Inspect for null expressions. Inspect the previous expressions which could have resulted in such null expressions. Add breakpoints and step through the program as appropriate. Use the debugger.
The null set is useful in mathematics and especially discrete mathematics. For instance, the natural numbers are formed by utilizing the empty set. Without it, the very foundation of number theory would not exist. The null set is also used to derive and prove multiple mathematical rules and formulas.
The solution is simple:
start service explicitly (e.g. in the Activity)
Intent serviceIntent = new Intent(this, TheService.class);
startService(serviceIntent);
in the Service onCreate()
you should
call startForeground(...)
Notification notification = ...;
startForeground(101, notification);
The wrong code that caused your issue was the instantiation of service:
mediaPlayerService = new MediaPlayerService();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With