每当我尝试通过蓝牙连接设备时,我的应用程序崩溃了。当然,我看到失败发生在connect.connect(设备),但它说它是一个空指针,即使我知道log.e,设备不是空的。我希望你们能告诉我如何解决这个问题或给我一个替代方案。提前谢谢!
修改
我收到了回复,因为我在Log.e和Textview中看到了这一点。
DeviceListActivity(我从中获取设备)
private AdapterView.OnItemClickListener mDeviceClickListener = new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> av, View v, int arg2, long arg3) {
mBtAdapter.cancelDiscovery(); // Cancel discovery because it's costly and we're about to connect
// Get the device MAC address, which is the last 17 chars in the View
String info = ((TextView) v).getText().toString();
String address = info.substring(info.length() - 17);
// Create the result Intent and include the MAC address
Intent data = new Intent();
data.putExtra("EXTRA_DEVICE_ADDRESS", address);
// Set result and finish(=close?) this Activity
setResult(Activity.RESULT_OK, data); //setResult(RESULT_OK, data); TODO Which one do we need?
finish();
}
};
TribotActivity(OnActivityResult所在的位置)
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_DEVICE_ADDRESS) { // Check which request we're responding to. When doing more requests a switch case is probably a nicer way of doing this.
if (resultCode == RESULT_OK) { // Make sure the request was successful
// Get the device MAC address
String address = data.getExtras().getString("EXTRA_DEVICE_ADDRESS");
// Get the BLuetoothDevice object
BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address);
Log.e(TAG, "onActivityResult: " + device.toString());
connect.connect(device);
textView = (TextView) findViewById(R.id.TV_MAC_address);
textView.setText("Device address: " + address);// Make sure the request was successful
}
}
}
连接课程(应该在哪里进行连接)
public synchronized void connect(BluetoothDevice device) {
Log.d(TAG, "connect to: " + device);
// Start the thread to connect with the given device
mConnectThread = new ConnectThread(device);
mConnectThread.start();
}
private class ConnectThread extends Thread {
private String TAG = "Connect: ";
private final BluetoothSocket mmSocket;
private final BluetoothDevice mmDevice;
public ConnectThread(BluetoothDevice device) {
// Use a temporary object that is later assigned to mmSocket,
// because mmSocket is final
BluetoothSocket tmp = null;
mmDevice = device;
// Get a BluetoothSocket to connect with the given BluetoothDevice
try {
// MY_UUID is the app's UUID string, also used by the server code TODO fix this comment
Log.i(TAG, "Device Name: " + mmDevice.getName());
Log.i(TAG, "Device UUID: " + mmDevice.getUuids()[0].getUuid());
tmp = device.createRfcommSocketToServiceRecord(mmDevice.getUuids()[0].getUuid());
} catch (IOException e) {
}
mmSocket = tmp;
}
public void run() {
// Cancel discovery because it will slow down the connection
if (mBluetoothAdapter.isDiscovering()) {
mBluetoothAdapter.cancelDiscovery();
}
try {
// Connect the device through the socket. This will block
// until it succeeds or throws an exception
mmSocket.connect();
} catch (IOException connectException) {
// Unable to connect; close the socket and get out
try {
mmSocket.close();
} catch (IOException closeException) {
}
return;
}
// Do work to manage the connection (in a separate thread)
// manageConnectedSocket(mmSocket);
}
/**
* Will cancel an in-progress connection, and close the socket
*/
public void cancel() {
try {
mmSocket.close();
} catch (IOException e) {
}
}
}
logcat的
04-12 14:50:00.258 31952-31952/com.hszuyd.noodle_.testing E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.hszuyd.noodle_.testing, PID: 31952
Theme: themes:{}
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { (has extras) }} to activity {com.hszuyd.noodle_.testing/com.hszuyd.noodle_.testing.TribotActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.hszuyd.noodle_.testing.Connect.connect(android.bluetooth.BluetoothDevice)' on a null object reference
at android.app.ActivityThread.deliverResults(ActivityThread.java:3733)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:3776)
at android.app.ActivityThread.-wrap16(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1412)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5461)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:133)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.hszuyd.noodle_.testing.Connect.connect(android.bluetooth.BluetoothDevice)' on a null object reference
at com.hszuyd.noodle_.testing.TribotActivity.onActivityResult(TribotActivity.java:102)
at android.app.Activity.dispatchActivityResult(Activity.java:6456)
at android.app.ActivityThread.deliverResults(ActivityThread.java:3729)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:3776)
at android.app.ActivityThread.-wrap16(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1412)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5461)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:133)