Android:sendBroadcast回调java.lang.NullPointerException中的未处理异常

时间:2015-02-26 20:05:52

标签: java android bluetooth

我正在尝试从服务向我的主要活动发送广播。出于某种原因,当我调用sendBroadcast时,我收到以下警告。

02-26 14:55:40.615  11079-11090/com.example.sdp11.wmd W/BluetoothGatt﹕ Unhandled exception in callback
java.lang.NullPointerException
        at android.content.ContextWrapper.sendBroadcast(ContextWrapper.java:372)
        at com.example.sdp11.wmd.BluetoothLEService.broadcastUpdate(BluetoothLEService.java:148)
        at com.example.sdp11.wmd.BluetoothLEService.access$100(BluetoothLEService.java:28)
        at com.example.sdp11.wmd.BluetoothLEService$1.onServicesDiscovered(BluetoothLEService.java:94)
        at android.bluetooth.BluetoothGatt$1.onSearchComplete(BluetoothGatt.java:301)
        at android.bluetooth.IBluetoothGattCallback$Stub.onTransact(IBluetoothGattCallback.java:215)
        at android.os.Binder.execTransact(Binder.java:412)
        at dalvik.system.NativeStart.run(Native Method)

我用来调用sendBroadcast函数的方法如下。

private void broadcastUpdate(final String action) {
    final Intent intent = new Intent(action);
    sendBroadcast(intent);
}

在我的蓝牙回叫中调用上述方法:

private final BluetoothGattCallback mGattCallback = new BluetoothGattCallback() {
    @Override
    public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
        //Connection established
        if (status == BluetoothGatt.GATT_SUCCESS
                && newState == BluetoothProfile.STATE_CONNECTED) {

            Log.e(TAG, "Connected Successfully");
            //Discover services
            gatt.discoverServices();

        } else if (status == BluetoothGatt.GATT_SUCCESS
                && newState == BluetoothProfile.STATE_DISCONNECTED) {
            Log.e(TAG, "Disconnected");
            //Handle a disconnect event
        }

        else {
            Log.e(TAG, "Connection state changed.  New state: " + newState);
        }
    }

    @Override
    // New services discovered
    public void onServicesDiscovered(BluetoothGatt gatt, int status) {
        if (status == BluetoothGatt.GATT_SUCCESS) {
            Log.e(TAG, "Services discovered");
            broadcastUpdate(ACTION_GATT_SERVICES_DISCOVERED);
        } else {
            Log.e(TAG, "Error, onServicesDiscovered received status: " + status);
        }
    }
}

非常感谢任何帮助。

修改

这是我主要活动的onCreate方法:

public static BluetoothLEService mBluetoothLEService;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    buildGoogleApiClient();
    createLocationRequest();

    mBluetoothLEService = new BluetoothLEService();
    Intent gattServiceIntent = new Intent(this, BluetoothLEService.class);
    bindService(gattServiceIntent, mServiceConnection, BIND_AUTO_CREATE);
}

private final ServiceConnection mServiceConnection = new ServiceConnection() {

    @Override
    public void onServiceConnected(ComponentName componentName, IBinder service) {
        mBluetoothLEService = ((BluetoothLEService.LocalBinder) service).getService();
        if (!mBluetoothLEService.initialize()) {
            Log.e(TAG, "Unable to initialize Bluetooth");
            finish();
        }
        // Automatically connects to the device upon successful start-up initialization.
        //mBluetoothLEService.connect(mDeviceAddress);
        Log.e(TAG, "Service Connected");
    }

    @Override
    public void onServiceDisconnected(ComponentName componentName) {
        mBluetoothLEService = null;
    }
};

2 个答案:

答案 0 :(得分:1)

我终于找到了问题。我在视图寻呼机中使用片段作为标签。我从片段的onCreate方法的主要活动中获得了mBluetoothLEService的实例。问题是这个mBluetoothLEService在这一点上仍然是空的。我最终只是从片段中使用了MainActivity.mBluetoothLEService.broadcastUpdate()。

答案 1 :(得分:0)

宣布ACTION_GATT_SERVICES_DISCOVERED在哪里?是否已初始化?确保它已被声明,初始化并且您可以访问它。