当我尝试打开蓝牙时应用程序崩溃

App crashes when I try to turn on Bluetooth

我正在尝试在我的应用程序中设置蓝牙。现在,我希望应用程序打开蓝牙(如果尚未打开)。每当我 运行 我的代码出现错误(如下)并且应用程序崩溃时 - 令人沮丧的部分是,我昨天工作正常,我不记得我必须改变什么才能让它不再工作.请帮我解决这个问题!这是我的代码和错误:

在 class 主要Activity:

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)


}

在 class 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()