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)
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.
Comments