NullPointerException at sendBroadcast() from Service to Activity

前端 未结 3 1660
离开以前
离开以前 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().

提交回复
热议问题