gpt4 book ai didi

android - 当我尝试开启蓝牙时,应用程式当机

转载 作者:行者123 更新时间:2023-12-02 07:19:15 25 4
gpt4 key购买 nike

我正在尝试在我的应用中设置蓝牙。现在,我希望该应用打开蓝牙(如果尚未打开)。每当我运行代码时,都会出现错误(如下),并且应用程序崩溃-令人沮丧的部分是,我昨天的工作正常,我不记得要更改为使其不再起作用的内容。请帮我解决这个问题!这是我的代码和错误:

在MainActivity类中:

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

//(I cut out miscellaneous layout code from here because it's not relevant)


try{
Thread.sleep(1000);
}catch(Exception e){
//
}


BluetoothHandler bluetoothHandler = new BluetoothHandler();
bluetoothHandler.bluetoothOn();//this line is the source of a current crash (this is line 825)


}

类BluetoothHandler中的类扩展了Activity:
public void bluetoothOn() {//turns on bluetooth

BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

try{
Thread.sleep(200);
}catch(Exception e){
//
}


/*
*Note: this app is designed specifically for one type of phone, so I don't check to see if it's
*bluetooth capable because it always will be (it's strictly for testing purposes)
*/

if (!bluetoothAdapter.isEnabled()) {
Intent toTurnOnBluetooth = new Intent(bluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(toTurnOnBluetooth, 22);//this is line 221




}
}

错误:
 E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.samples.flironecamera, PID: 31190
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.samples.flironecamera/com.samples.flironecamera.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.app.ActivityThread$ApplicationThread android.app.ActivityThread.getApplicationThread()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3135)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3278)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1969)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7124)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:494)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:975)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.app.ActivityThread$ApplicationThread android.app.ActivityThread.getApplicationThread()' on a null object reference
at android.app.Activity.startActivityForResult(Activity.java:4692)
at android.app.Activity.startActivityForResult(Activity.java:4649)
at com.samples.flironecamera.BluetoothHandler.bluetoothOn(BluetoothHandler.java:221)
at com.samples.flironecamera.MainActivity.onCreate(MainActivity.java:825)
at android.app.Activity.performCreate(Activity.java:7335)
at android.app.Activity.performCreate(Activity.java:7326)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1275)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3115)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3278) 
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78) 
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108) 
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1969) 
at android.os.Handler.dispatchMessage(Handler.java:106) 
at android.os.Looper.loop(Looper.java:214) 
at android.app.ActivityThread.main(ActivityThread.java:7124) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:494) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:975) 

最佳答案

请确保您已启用蓝牙适配器

BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); 
mBluetoothAdapter.enable()

关于android - 当我尝试开启蓝牙时,应用程式当机,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61935883/

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