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

标签 android bluetooth crash

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

在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/

相关文章:

android - Xamarin Realm 没有 realobject 链接器剥离了它们

android - android中paypal api的问题

android - 蓝牙创建套接字但在 connect() 处阻塞 ANDROID

iphone - 检查蓝牙是否已启用?

安卓蓝牙 : "Scan failed, reason app registration failed for UUID"

javascript - 补间JS导致WebGL上下文丢失

android - 从 Play 服务单体中过滤资源以使您的 APK 更小

Android res values-v11 或更新版本

visual-studio-2010 - x64 上的 Visual Studio 2010 是否经常崩溃?或者只是在我的电脑上?

ios - 如何在 iOS 中获得 C 函数崩溃?