App crashes when I try to turn on Bluetooth

Daniel H.

I'm trying to set up Bluetooth in my app. For now, I want the app to turn on Bluetooth if it's not already. Whenever I run my code I get an error (below) and the app crashes - the frustrating part is, I had this working fine yesterday and I don't recall what I must've changed to make it not work anymore. Please help me fix this! Here is my code and errors:

in class 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)


}

in class BluetoothHandler extends 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




    }
}

Error:

 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) 
Android_id

Please make sure you have enabled the bluetooth adapter

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

When I try to set a clickListener, app crashes

App crashes when I try to logout

Android app crashes when i try to reLogin

App crashes when i try to run this page

App crashes when I try to open a Fragment

App crashes when i try to set onClickListener

My app crashes when I try to add Image Button to it

When I Try to Change the Color of SnackBar TextView App Crashes

MapFragment make app crashes at runtime when I try to setMapType

my app crashes when I try to download an image

App crashes when I try to insert data into database

App crashes when I try to edit an EditText view

App crashes when I try to read data from Cloud Firestore

Android:App crashes when i try to retrieve data from intent

My app crashes when i try to move to a particular layout

App crashes when i try to add data to my database

App Crashes when try to set the maximumValue to the UISlider

Why can I save an enum to Firestore, but it crashes the app when I try to retrieve the same?

Application crashes when i try to debug

Roslyn crashes when I try to run a script

Pythonw crashes when I try to run a file

When I try to add more than 4 images to the layout, the app crashes

App crashes when I try to launch in Android Kitkat 4.4.4 but works fine in Android 5 & 6 versions

why does my app crashes when i try adding a radio button to the radio group?

my app crashes eveytime I try to use app.use

App crashes when try to get `BottomNavigationView`'s layout params

Heroku app crashes every time I try to open

C#. My Windows Forms App crashes when I try to check whether or not the user exists in my SQL database

App crashes when I add an onClickListener to ImageView