【Android studio虚拟机点击按钮出现闪退】有奖问答

//获取本地蓝牙适配器信息
bluetoothAdapter=BluetoothAdapter.getDefaultAdapter();
//打开蓝牙
if(!bluetoothAdapter.isEnabled()){
    intent=new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
    startActivityForResult(intent,1);//1表示打开蓝牙
}
@SuppressLint("MissingPermission")
Set<BluetoothDevice> pairedDevices=bluetoothAdapter.getBondedDevices();
readyDevices = new ArrayList();
//判断有没有以配对的设备
if(pairedDevices!=null&&pairedDevices.size()>0) {
    for (BluetoothDevice device : pairedDevices) {
        readyDevices.add(device);
        devicesNames.add(device.getName());
        btnames = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, devicesNames);
    }
    btList.setAdapter(btnames);
}else{
    Toast.makeText(this,"没有设备已配对!",Toast.LENGTH_SHORT).show();
}
//列表项目蓝牙设备名称点击,连接
btList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    private int position;

这是日志
04/15 15:43:14: Launching 'app' on Pixel 5 API 26.
Install successfully finished in 867 ms.
$ adb shell am start -n "com.example.ly/com.example.ly.MainActivity" -a android.intent.action.MAIN -c android.intent.category.LAUNCHER
Connected to process 22606 on device 'Pixel_5_API_26 [emulator-5554]'.
Capturing and displaying logcat messages from application. This behavior can be disabled in the "Logcat output" section of the "Debugger" settings page.
I/zygote: Late-enabling -Xcheck:jni
W/zygote: Unexpected CPU variant for X86 using defaults: x86
D/OpenGLRenderer: HWUI GL Pipeline
D/: HostConnection::get() New Host Connection established 0x9f464680, tid 22637
I/OpenGLRenderer: Initialized EGL, version 1.4
D/OpenGLRenderer: Swap behavior 1
W/OpenGLRenderer: Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without...
D/OpenGLRenderer: Swap behavior 0
D/EGL_emulation: eglCreateContext: 0x9f705120: maj 2 min 0 rcv 2
D/EGL_emulation: eglMakeCurrent: 0x9f705120: ver 2 0 (tinfo 0x9f703280)
D/EGL_emulation: eglMakeCurrent: 0x9f705120: ver 2 0 (tinfo 0x9f703280)
E/BluetoothAdapter: Bluetooth binder is null
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.ly, PID: 22606
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.ly/com.example.ly.BluetoothActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.bluetooth.BluetoothAdapter.isEnabled()' on a null object reference
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2817)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
        at android.app.ActivityThread.-wrap11(Unknown Source:0)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
        at android.os.Handler.dispatchMessage(Handler.java:105)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6541)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.bluetooth.BluetoothAdapter.isEnabled()' on a null object reference
        at com.example.ly.BluetoothActivity.onCreate(BluetoothActivity.java:60)
        at android.app.Activity.performCreate(Activity.java:6975)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1213)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2770)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892) 
        at android.app.ActivityThread.-wrap11(Unknown Source:0) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593) 
        at android.os.Handler.dispatchMessage(Handler.java:105) 
        at android.os.Looper.loop(Looper.java:164) 
        at android.app.ActivityThread.main(ActivityThread.java:6541) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767) 
I/Process: Sending signal. PID: 22606 SIG: 9
 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值