search

Home  >  Q&A  >  body text

android service和activity通信sendBroadcast报错nullpointerexeption

service中总是获取不到context,sendBroadcast报空指针异常

`public class HandleMessage extends Service{

private  String  qrcontent = null;
private Context context = this;

@Override
public void onCreate() {
    super.onCreate();
    context = this;
}

public void doSomething(final String content){

    qrcontent = content;
    new Thread(new Runnable() {
        @Override
        public void run() {
                //发送广播
                Intent intent=new Intent();
                intent.putExtra("qrcontent", qrcontent);
                intent.setAction("com.HandleMessage");
                if(intent != null){
                    LocalBroadcastManager.getInstance(context).sendBroadcast(intent);
                }else{
                    Log.e("---","null intent");
                }
            }
    }).start();
}

}`

ringa_leeringa_lee2771 days ago629

reply all(2)I'll reply

  • PHPz

    PHPz2017-04-18 09:19:10

    Where is the service started?

    reply
    0
  • 巴扎黑

    巴扎黑2017-04-18 09:19:10

    Why open a thread to broadcast?

    reply
    0
  • Cancelreply