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

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

membalas semua(2)
PHPzhong

Di manakah perkhidmatan bermula

巴扎黑

Kenapa buka thread untuk disiarkan?

Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan