来自IntentService的通知 - 获取Context时的NullPointerException

Cha*_*nya 7 android android-service android-notifications

我无法从中生成通知(在通知区域中)IntentService.我得到了一段NullPointerException时间NotificationManager.问题在于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)
Run Code Online (Sandbox Code Playgroud)

代码行是:

mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Run Code Online (Sandbox Code Playgroud)

我试过了getApplicationContext(),getBaseContext()但无济于事.

有人可以告诉我这里有什么问题吗?如何从IntentService生成通知?


附加信息:我的应用程序中
也有一个Service,并且来自那里的通知正常工作.该IntentService不是由活动时间; 它由服务部门启动.

小智 21

将您的调用getSystemService移出构造函数并进入onCreate.

该基地ContextContextWrapper还没有确定的是,这是造成NullPointerException.