android service和activity通信sendBroadcast报错nullpointerexeption
ringa_lee
ringa_lee 2017-04-18 09:17:10
0
2
627

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_lee
ringa_lee

ringa_lee

reply all(2)
PHPzhong

Where is the service started?

巴扎黑

Why open a thread to broadcast?

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template