gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 02:19:53 25 4
gpt4 key购买 nike

我正在尝试从服务向我的主要 Activity 发送广播。出于某种原因,当我调用 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);
}
}
}

如有任何帮助,我们将不胜感激。

编辑

这是在我的主要 Activity 的 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;
}
};

最佳答案

我终于想通了这个问题。我在选项卡的 View 寻呼机中使用 fragment 。我从 fragment 的 onCreate 方法中的主要 Activity 中获取了 mBluetoothLEService 的实例。问题是此时此 mBluetoothLEService 仍为 null。我最终只使用了 fragment 中的 MainActivity.mBluetoothLEService.broadcastUpdate() 。

关于java - Android:回调 java.lang.NullPointerException 中的 sendBroadcast 未处理的异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28751760/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com