Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/365.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android:sendBroadcast回调java.lang.NullPointerException中未处理的异常_Java_Android_Bluetooth - Fatal编程技术网

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

Android:sendBroadcast回调java.lang.NullPointerException中未处理的异常,java,android,bluetooth,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(Conte

我正在尝试将广播从服务发送到我的主要活动。由于某种原因,当我调用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;
    }
};

关贸总协定服务行动在哪里宣布?它已经初始化了吗?确保它已声明、初始化并且您有权访问它。

我终于解决了问题。我在查看页面中使用片段作为选项卡。我从片段的onCreate方法中的主活动中获得了mBluetoothLEService的一个实例。问题是这个mBluetoothLEService此时仍然为空。最后,我只使用了片段中的MainActivity.mBluetoothLEService.broadcastUpdate()

是的,ACTION\u GATT\u SERVICES\u DISCOVERED是一个定义为“.ACTION\u GATT\u SERVICES\u DISCOVERED”的字符串。如果从活动调用sendBroadcast(null),我会收到相同的错误。您确定在调用sendBroadcast()时您的意图不为null吗?代码看起来像是引用了一个非null对象,但错误表明不是这样。调用sendBroadcast()时是否还有其他位置?是的,我检查了调用时意图是否为null,而不是。我还添加了一条日志消息,以从意图中获取操作,这也是正确的。我也查看了这个答案。我将通过bindService调用编辑我的原始问题。