带设备的Android蓝牙错误填充列表

时间:2017-10-24 09:06:36

标签: android bluetooth nullpointerexception

早上好,

我在使用蓝牙设备填写列表时遇到问题。 智能手机正在使用蓝牙搜索附近的蓝牙设备并将其添加到listView。如果设备已与手机配对,则“(已配对)”字符串将添加到设备名称中。

在找到最后一个BT-Devices之后,调试器跳转到looper类并停留在:

public static void loop() {
        throw new RuntimeException("Stub!");
    }

之后我收到此错误:

10-24 10:42:40.433 19279-19279/com.example.tut1 E/AndroidRuntime: FATAL EXCEPTION: main
                                                              Process: com.example.tut1, PID: 19279
                                                              java.lang.RuntimeException: Error receiving broadcast Intent { act=android.bluetooth.device.action.FOUND flg=0x10 (has extras) } in com.example.tut1.Bluetooth$1@d32059a
                                                                  at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:1132)
                                                                  at android.os.Handler.handleCallback(Handler.java:751)
                                                                  at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                  at android.os.Looper.loop(Looper.java:154)
                                                                  at android.app.ActivityThread.main(ActivityThread.java:6121)
                                                                  at java.lang.reflect.Method.invoke(Native Method)
                                                                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:889)
                                                                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:779)
                                                               Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference
                                                                  at com.example.tut1.Bluetooth$1.onReceive(Bluetooth.java:121)
                                                                  at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:1122)
                                                                  at android.os.Handler.handleCallback(Handler.java:751) 
                                                                  at android.os.Handler.dispatchMessage(Handler.java:95) 
                                                                  at android.os.Looper.loop(Looper.java:154) 
                                                                  at android.app.ActivityThread.main(ActivityThread.java:6121) 
                                                                  at java.lang.reflect.Method.invoke(Native Method) 
                                                                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:889) 
                                                                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:779)

这是我的代码:

private void init(){

    listView = (ListView)findViewById(R.id.listView);
    listView.setOnItemClickListener(this);
    listAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,0);
    listView.setAdapter(listAdapter);
    btAdapter = BluetoothAdapter.getDefaultAdapter();
    pairedDevices = new ArrayList<String>();
    filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
    devices = new ArrayList<BluetoothDevice>();


    receiver = new BroadcastReceiver(){
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (BluetoothDevice.ACTION_FOUND.equals(action)){
                BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                devices.add(device);
                String s = "";
                for(int a=0;a<pairedDevices.size();a++){
                    if (device.getName().equals(pairedDevices.get(a))){
                        //append
                        s = "(Paired)";
                        break;
                    }
                }
                listAdapter.add(device.getName()+" "+s+" "+"\n"+device.getAddress());

            }else if (BluetoothAdapter.ACTION_DISCOVERY_STARTED.equals(action)){

            }else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)){

            }else if (BluetoothAdapter.ACTION_STATE_CHANGED.equals(action)){
                if (btAdapter.getState() == btAdapter.STATE_OFF){
                    turnOnBT();
                }
            }
        }

    };

    registerReceiver(receiver, filter);
    IntentFilter filter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
    registerReceiver(receiver, filter);
    filter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
    registerReceiver(receiver, filter);
    filter = new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED);
}

非常感谢&amp;有一个伟大的!

0 个答案:

没有答案