java.lang.RuntimeException : Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { (has extras) }} to Activity

标签 java android bluetooth runtimeexception

我正在尝试制作一个蓝牙应用程序,并且正在遵循developer.android 中的“指南”,但是每当我尝试 getRemoteDevice 时,我的应用程序都会崩溃。肯定有什么地方出了问题,但我就是不知道出什么问题。

TribotActivity(onActivityResult 类)

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) {
            connectDevice(data, true);// Make sure the request was successful
        } else {
            Toast.makeText(getApplicationContext(), "Failed to get MAC address from ", Toast.LENGTH_SHORT).show();    //TODO Remove this when we've successfully sent through the address
        }
    }
}

private void connectDevice(Intent data, boolean secure) {
    // Get the device MAC address
    String address = data.getExtras()
            .getString(DeviceListActivity.EXTRA_DEVICE_ADDRESS);
    // Get the BluetoothDevice object
    BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address);
    // Attempt to connect to the device
    mConnect.connect(device, secure);
}

DeviceListActivity

public static String EXTRA_DEVICE_ADDRESS = "device_address";

private AdapterView.OnItemClickListener mDeviceClickListener = new AdapterView.OnItemClickListener() {
    public void onItemClick(AdapterView<?> av, View v, int arg2, long arg3) {
        // Cancel discovery because it's costly and we're about to connect
        mBtAdapter.cancelDiscovery();

        // 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(RESULT_OK, data);
        finish();
    }
};

我敢打赌,设备不会以某种方式发送到 TribotActivity。 这是我的 logcat :)

Logcat

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 'android.bluetooth.BluetoothDevice android.bluetooth.BluetoothAdapter.getRemoteDevice(java.lang.String)' 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:117)
                                                                         Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.bluetooth.BluetoothDevice android.bluetooth.BluetoothAdapter.getRemoteDevice(java.lang.String)' on a null object reference
                                                                            at com.hszuyd.noodle_.testing.TribotActivity.connectDevice(TribotActivity.java:93)
                                                                            at com.hszuyd.noodle_.testing.TribotActivity.onActivityResult(TribotActivity.java:71)
                                                                            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:117) 

最佳答案

我看不到 mBluetoothAdapter 的声明在哪里,但根据异常堆栈,它似乎为 null,因此它试图在 null 对象上调用方法 getRemoteDevice()。

关于java.lang.RuntimeException : Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { (has extras) }} to Activity,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36422520/

相关文章:

java - Jenkins Build 文件夹中缺少 log4j.properties

java - JavaFX 中使用默认 modena 样式的按钮宽度

java - 在数字中插入点 - Android

java - ListView.setAdapter(ArrayAdapter) 使应用程序崩溃

java - InputStream.read() 在我的缓冲区中丢失或添加了一个值 - 这是怎么回事?

java - REST 确保 GET 在通用后不起作用

java - 在 Jooq 中缓存查询

java - 将 EditText 可见性更改为隐藏

android - 如何下载现有项目的 google services.json 文件?

android - 如何知道特定蓝牙设备何时连接?