sendBroadcast()中的NullPointerException,从Service到Activity

时间:2012-03-22 16:48:46

标签: android broadcastreceiver

我有一个Service类和一个主Acitivity类,它应该使用方法sendBroadcast从Service类接收广播。

从我的sendBroadcast类运行方法Service时崩溃。

这是我的服务类(已编辑)的一部分:

    public static final int STATE_CONNECTING = 1;

    public static final String BT_CONNECTING = "com.android.mypackage.action.BTService.BT_CONNECTING";

    private final IBinder mBinder = new LocalBinder();

    public class LocalBinder extends Binder() {
            BTService getService() {
                    return BTService.this;
            }
    }

    @Override
    public IBinder onBind(Intent intent) {
            return mBinder;
    }

    public synchronized void setState(int state) {
            mState = state;
            if ( state == STATE_CONNECTING ) {
                    Intent myIntent = new Intent(BT_CONNECTING);
                    try {
                            sendBroadcast(myIntent);
                    }
                    catch (NullPointerException e) {}
            }
    }

这是我的Activity类的一部分,它应该接收广播的意图( EDITED ):

    private BroadcastReceiver receiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                    if(intent.getAction().equals(BTService.BT_CONNECTING))
                            mState = STATE_CONNECTING;
                    }
            };

    private ServiceConnection mConnection = new ServiceConnection() {
            public void onServiceConnected(ComponentName className, IBinder service) {
                    LocalBinder binder = (LocalBinder) service;
                    myService = binder.getService();
                    mBound = true;
            }

            public void onServiceDisconnected(ComponentName arg0) {
                    mBound = false;
            }
    };

    @Override
    public void onCreate(Bundle savedInstanceState) {
            Intent intent = new Intent(this, BTService.class);
            myService.bindService(intent, mConnection, Context.BIND_AUTO_CREATE);

            IntentFilter filter = new IntentFilter(BTService.BTConnecting);
            registerReceiver(receiver, filter);
    }

调用sendBroadcast(intent)方法时出现NullPointerException。任何有关解决此问题的提示都非常感谢。

这是日志:

    FATAL EXCEPTION: main
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.android.mypackage/com.android.mypackage.MyActivity}: java.lang.NullPointerException
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2787)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2803)
    at android.app.ActivityThread.access$2300(ActivityThread.java:135)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2136)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:144)
    at android.app.ActivityThread.main(ActivityThread.java:4937)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:521)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
    at dalvik.system.NativeStart.main(Native Method)
    Caused by: java.lang.NullPointerException
    at android.content.ContextWrapper.sendBroadcast(ContextWrapper.java:271)
    at com.android.mypackage.BTService.setState(BTService.java:68)
    at com.android.mypackage.BTService.connect(BTService.java:90)
    at com.android.mypackage.MyActivity.onCreate(MyActivity.java:78)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1069)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2751)

3 个答案:

答案 0 :(得分:4)

尝试将sendBroadcast来电更改为:context.sendBroadcast(intent);

答案 1 :(得分:3)

从您的堆栈跟踪中,您可能会以某种方式直接引用您的BTService服务。由于你削减了onCreate()短片,我不能确定你是怎么做的,但我会猜测。

您是否直接在活动中实例化此服务(使用new BTService())?如果是这样,那么您收到此错误的原因是因为您的服务没有绑定到它的上下文。您必须致电startService()bindService()

让Android为您创建服务

答案 2 :(得分:0)

尝试添加myIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK)