Android: Using onStart() method in Bluetooth application

Posted by Nii on Stack Overflow See other posts from Stack Overflow or by Nii
Published on 2010-04-19T01:12:33Z Indexed on 2010/04/19 1:13 UTC
Read the original article Hit count: 570

Hello,

I am getting a nullpointer exception when my onStart() method is called. Here is the breakdown of my Android app:

Opening the app brings a user to the homescreen: The user is then presented with the first 6 icons to choose from. When the user presses the "Sugar" icon it takes them to the SugarTabActivity.

The SugarTabActivity is a Tabbed layout with two tabs. I'm concerned with the first tab. The first tab calls the getDefaultAdapter() method in its onCreate() method. Once it calls this, it checks if the bluetooth adapter is null on the phone, and if its null it shows a toast saying "Bluetooth is not available". This works just fine. Then I call the onStart() method. In the onStart() method I check if bluetooth is enabled, and if it isnt, then I start a new activity from the BluetoothAdapter enable bluetooth intent; otherwise, I start my bluetooth service.

The exact error I'm getting is 04-19 00:44:45.674: ERROR/AndroidRuntime(225): Caused by: java.lang.NullPointerException 04-19 00:44:45.674: ERROR/AndroidRuntime(225): at com.nii.glucose.Glucose.onStart(Glucose.java:313).


Heading

Override public void onCreate(Bundle icicle) { super.onCreate(icicle); if(D) Log.d(TAG, "+++ ON CREATE +++"); setContentView(R.layout.glucose_layout); mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); if(mBluetoothAdapter==null){ Toast.makeText(this, "Bluetooth not available", Toast.LENGTH_LONG).show(); //finish(); return; } }

Override public void onStart() { super.onStart(); if(D) Log.e(TAG, "++ ON START ++");

    // If BT is not on, request that it be enabled.
    // setupChat() will then be called during onActivityResult
    if (!mBluetoothAdapter.isEnabled()) {
        Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
        startActivityForResult(enableIntent, REQUEST_ENABLE_BT);
    // Otherwise, setup the chat session
    } else {
        if (mGlucoseService == null) 
         mGlucoseService = new BluetoothService(this, mHandler);
    }

}

@Override public synchronized void onResume(){ super.onResume(); if(D) Log.e(TAG,"==== ON RESUME ======"); // Performing this check in onResume() covers the case in which BT was // not enabled during onStart(), so we were paused to enable it... // onResume() will be called when ACTION_REQUEST_ENABLE activity returns. if (mGlucoseService != null) { // Only if the state is STATE_NONE, do we know that we haven't started already if (mGlucoseService.getState() == BluetoothService.STATE_NONE) { // Start the Bluetooth chat services mGlucoseService.start(); } } }

@Override public synchronized void onPause(){ super.onPause(); //isActive.set(false); if(D) Log.e(TAG,"==== ON PAUSE ======"); }

@Override public void onDestroy() { super.onDestroy(); // Stop the Bluetooth chat services if (mGlucoseService != null) mGlucoseService.stop(); if(D) Log.e(TAG, "--- ON DESTROY ---"); }

© Stack Overflow or respective owner

Related posts about android

Related posts about bluetooth