Notification from IntentService - NullPointerException on getting context

I cannot generate a notification (in the notification area) from an IntentService . I get a NullPointerException by getting a NotificationManager . The problem is Context.

 06-01 16:46:05.910: ERROR/AndroidRuntime(14745): Caused by: java.lang.NullPointerException 06-01 16:46:05.910: ERROR/AndroidRuntime(14745): at android.content.ContextWrapper.getSystemService(ContextWrapper.java:363) 06-01 16:46:05.910: ERROR/AndroidRuntime(14745): at com.Android.Main1.FileUploaderService.<init>(FileUploaderService.java:71) 

Line of code:

 mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 

I tried getApplicationContext() , getBaseContext() , but to no avail.

Can someone please let me know what the problem is? How to create notifications from IntentService?


Additional Information:
I also have a Service in my application, and notifications from there work fine. IntentService does not start Activity; it is launched by the Service.

+6
source share
1 answer

Move the getSystemService call from the constructor and to onCreate .

The Context base in ContextWrapper has not yet been set, which raises a NullPointerException .

+19
source

Source: https://habr.com/ru/post/889582/


All Articles