意图为空的Android IntentService触发了 [英] Android IntentService triggered with null intent
问题描述
我在Crashlytics中遇到崩溃:
I'm seeing a crash in Crashlytics:
致命异常:java.lang.NullPointerException:尝试调用 虚方法'int android.content.Intent.getIntExtra(java.lang.String,int)'上的null 对象参考 在com.myapp.APKOfferQueueManagerIntentService.onHandleIntent(SourceFile:71) 在android.app.IntentService $ ServiceHandler.handleMessage(IntentService.java:65) 在android.os.Handler.dispatchMessage(Handler.java:102) 在android.os.Looper.loop(Looper.java:135) 在android.os.HandlerThread.run(HandlerThread.java:61)
Fatal Exception: java.lang.NullPointerException: Attempt to invoke virtual method 'int android.content.Intent.getIntExtra(java.lang.String, int)' on a null object reference at com.myapp.APKOfferQueueManagerIntentService.onHandleIntent(SourceFile:71) at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:65) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:135) at android.os.HandlerThread.run(HandlerThread.java:61)
怎么可能以空意图触发IntentService?
How can it be that an IntentService is being triggered with a null intent?
推荐答案
IntentService
可能像其他所有服务一样被停止,但是它将由系统"重新启动,直到onHandleIntent()
完成其工作.在这种情况下,文档说 intent 参数
An IntentService
may be stopped like every other service, but it will be restarted by "the system" until onHandleIntent()
has finished its work. In this case, the documentation says the intent parameter
...如果服务在进程消失后重新启动,则可能为空
... may be null if the service is being restarted after its process has gone away
要始终获取原始的Intent
作为参数,请使用
To always get the original Intent
as a parameter, use
setIntentRedelivery(true);
在IntentService
的构造函数中.
请注意
如果发送了多个Intent,则只能保证最新的Intent被重新发送.
If multiple Intents have been sent, only the most recent one is guaranteed to be redelivered.
这篇关于意图为空的Android IntentService触发了的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!