NullPointerException at sendBroadcast() from Service to Activity

前端 未结 3 1661
离开以前
离开以前 2020-12-21 22:51

I have a Service class and an main Acitivity class which is supposed to receive broadcasts from the Service class with method sendBroadcast.

<
相关标签:
3条回答
  • 2020-12-21 23:05

    From your stack trace, it appears you are somehow directly referencing your BTService service. Since you cut your onCreate() short I can't be certain how you are doing it, but I will take a guess.

    Did you instantiate this service directly inside of your activity (use new BTService())? If so, then the reason you are getting this error is because your Service has no context bound to it. You must let Android create your service for you by calling startService() or bindService().

    0 讨论(0)
  • 2020-12-21 23:09

    Try to add myIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK)

    0 讨论(0)
  • 2020-12-21 23:16

    Try changing your sendBroadcast call to: context.sendBroadcast(intent);

    0 讨论(0)
提交回复
热议问题