android Service onStartCommand intent为null

log

D/AndroidRuntime(19002): Shutting down VM
W/dalvikvm(19002): threadid=1: thread exiting with uncaught exception (group=0xb24c4fb0)
E/AndroidRuntime(19002): FATAL EXCEPTION: main
E/AndroidRuntime(19002): java.lang.RuntimeException: Unable to start service com.hsae.auto.datt.radio.RadioService@b2842f20 with null: java.lang.NullPointerException
E/AndroidRuntime(19002):        at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2673)
E/AndroidRuntime(19002):        at android.app.ActivityThread.access$1900(ActivityThread.java:141)
E/AndroidRuntime(19002):        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1331)
E/AndroidRuntime(19002):        at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime(19002):        at android.os.Looper.loop(Looper.java:137)
E/AndroidRuntime(19002):        at android.app.ActivityThread.main(ActivityThread.java:5041)
E/AndroidRuntime(19002):        at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(19002):        at java.lang.reflect.Method.invoke(Method.java:511)
E/AndroidRuntime(19002):        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:800)
E/AndroidRuntime(19002):        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:565)
E/AndroidRuntime(19002):        at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime(19002): Caused by: java.lang.NullPointerException
E/AndroidRuntime(19002):        at com.hsae.auto.datt.radio.RadioService.onStartCommand(RadioService.java:101)
E/AndroidRuntime(19002):        at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2656)
E/AndroidRuntime(19002):        ... 10 more

onStartCommand的intent为空才,报NullPointException

 

 原因:

intent的参数是null,原因是这个intent参数是通过startService(Intent)方法所传递过来的,但是如果Service在你的进程退出后有可能被系统自动重启,这个时候intent就会是null.
解决方法:
所以在使用intent前需要判断一下是否为空
还有另外一种解决方法:
如果实现 onStartCommand去调度异步工作或者其他的线程,有必要设置START_FLAG_REDELIVERY让系统重发intentservice以便service被killed后不会丢失intent数据。

 

Java代码 

return super.onStartCommand(intent, Service.START_REDELIVER_INTENT, startId); 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值