本文介绍了来自IntentService的通知-NullPointerException,同时获取上下文的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法从IntentService生成通知(在通知区域中).获取NotificationManager时得到一个NullPointerException.问题出在上下文.

I am unable to generate the notification (in the notification area) from an IntentService. I get a NullPointerException while obtaining NotificationManager. The problem is with 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)

代码行是:

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

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

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

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

Could someone please let me know what is the problem here? How do I generate notifications from an IntentService?

其他信息:
我的应用程序中也有一个Service,并且来自那里的通知可以正常工作. IntentService不是由Activity启动的;它由服务启动.

Additional Info:
I also have a Service in my app, and notifications from there work properly. The IntentService is not started by an Activity; it is started by the Service.

推荐答案

将对getSystemService的调用移出构造函数,并移至onCreate.

Move your call to getSystemService out of the constructor and into onCreate.

ContextWrapper中的基本Context尚未设置,这导致NullPointerException.

The base Context in the ContextWrapper has not been set yet, which is causing the NullPointerException.

这篇关于来自IntentService的通知-NullPointerException,同时获取上下文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-02 01:23