2

I am attempting to utilize an Activity to enable Bluetooth on an Android device. The following is the class I have extended the Activity class with. I am receiving a NullPointerException error when the startActivityForResult() method is called. Any suggestions? Thank you!

public class Activities extends Activity{
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }
    protected void connectBluetooth(){
        Intent intentBluetooth = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);

        startActivityForResult(intentBluetooth, 0); 
    }
}

The log output is as follows:

?:??: W/?(?): [ 09-09 18:08:38.497 15231:15246 W/System.err ]
?:??: W/?(?): java.lang.NullPointerException
?:??: W/?(?): [ 09-09 18:08:38.497 15231:15246 W/System.err ]
?:??: W/?(?):   at android.app.Activity.startActivityForResult(Activity.java:3351)
?:??: W/?(?): [ 09-09 18:08:38.497 15231:15246 W/System.err ]
?:??: W/?(?):   at android.app.Activity.startActivityForResult(Activity.java:3312)
?:??: W/?(?): [ 09-09 18:08:38.497 15231:15246 W/System.err ]
?:??: W/?(?):   at com.eyesore.bluetooth.Activities.connectBluetooth(Activities.java:28)
?:??: W/?(?): [ 09-09 18:08:38.497 15231:15246 W/System.err ]
?:??: W/?(?):   at com.eyesore.bluetooth.BluetoothModule.example(BluetoothModule.java:77)
?:??: W/?(?): [ 09-09 18:08:38.497 15231:15246 W/System.err ]
?:??: W/?(?):   at org.appcelerator.kroll.runtime.v8.V8Object.nativeFireEvent(Native Method)
?:??: W/?(?): [ 09-09 18:08:38.497 15231:15246 W/System.err ]
?:??: W/?(?):   at org.appcelerator.kroll.runtime.v8.V8Object.fireEvent(V8Object.java:60)
?:??: W/?(?): [ 09-09 18:08:38.497 15231:15246 W/System.err ]
?:??: W/?(?):   at org.appcelerator.kroll.KrollProxy.doFireEvent(KrollProxy.java:636)
?:??: W/?(?): [ 09-09 18:08:38.497 15231:15246 W/System.err ]
?:??: W/?(?):   at org.appcelerator.kroll.KrollProxy.handleMessage(KrollProxy.java:831)
?:??: W/?(?): [ 09-09 18:08:38.497 15231:15246 W/System.err ]
?:??: W/?(?):   at org.appcelerator.titanium.proxy.TiViewProxy.handleMessage(TiViewProxy.java:307)
?:??: W/?(?): [ 09-09 18:08:38.497 15231:15246 W/System.err ]
?:??: W/?(?):   at android.os.Handler.dispatchMessage(Handler.java:95)
?:??: W/?(?): [ 09-09 18:08:38.497 15231:15246 W/System.err ]
?:??: W/?(?):   at android.os.Looper.loop(Looper.java:137)
?:??: W/?(?): [ 09-09 18:08:38.497 15231:15246 W/System.err ]
?:??: W/?(?):   at org.appcelerator.kroll.KrollRuntime$KrollRuntimeThread.run(KrollRuntime.java:104)
?:??: W/?(?): [ 09-09 18:08:38.497 15231:15231 E/TiJSError ]
?:??: W/?(?): (main) [6,293329] ----- Titanium Javascript Runtime Error -----
?:??: W/?(?): [ 09-09 18:08:38.497 15231:15231 E/TiJSError ]
?:??: W/?(?): (main) [0,293329] - In app.js:71,12
?:??: W/?(?): [ 09-09 18:08:38.497 15231:15231 E/TiJSError ]
?:??: W/?(?): (main) [0,293329] - Message: Uncaught Error: Java Exception occurred
?:??: W/?(?): [ 09-09 18:08:38.497 15231:15231 E/TiJSError ]
?:??: W/?(?): (main) [0,293329] - Source:   bluetooth.example();
?:??: W/?(?): [ 09-09 18:08:38.517 15231:15246 E/V8Exception ]
?:??: W/?(?): Exception occurred at app.js:71: Uncaught Error: Java Exception occurred

Manifest file below -

<manifest>
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
    <uses-permission android:name="android.permission.BLUETOOTH"/>
    <uses-permission android:name="android.bluetooth.adapter.action.REQUEST_ENABLE"/>
        <application>
            <activity android:name=".Activities" android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
    </application>            
</manifest>
ColdFire
  • 6,764
  • 6
  • 35
  • 51
Garrett
  • 81
  • 2
  • 6
  • Can you edit your post to show the LogCat output from the crash, please? – Cat Sep 09 '12 at 19:23
  • Certainly. Please see the updated post. – Garrett Sep 09 '12 at 19:29
  • Alright, when do you call `connectBluetooth`? You should be calling it from `onCreate`. – Cat Sep 09 '12 at 19:33
  • I see. I was calling it from another class where I instantiated the Activities object: Activities testActivities = new Activities(); testActivities.connectBluetooth(); – Garrett Sep 09 '12 at 19:39
  • Oh my, no, you should never ever ever ever create an `Activity` using `new`. Ever. I'll try to summarize this in an answer for you... – Cat Sep 09 '12 at 19:40

1 Answers1

0

Based on the comments in the OP, you are not starting the Activity properly. You should never ever ever start an Activity using new.

Firstly, your Activites should look like this, calling connectBluetooth() on its own.

public class Activities extends Activity{
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        connectBluetooth();
    }
    protected void connectBluetooth(){
        Intent intentBluetooth = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);

        startActivityForResult(intentBluetooth, 0); 
    }
}

When an Android app starts, it runs the main Activity, which is defined in the manifest. if Activities is your main Activity, then no further code will be required.

However, I get the hunch that this is a second Activity, which is started from somewhere else. So let's say you have this:

public class MainActivity extends Activity{
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Activities testActivities = new Activities(); // This is bad!
        testActivities.connectBluetooth(); // This too!
    }
}

What you should be doing instead, is this:

public class MainActivity extends Activity{
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        startActivity(new Intent(this, Activities.class)); // Starts an Activity properly
    }
}

This will start your Activity, call onCreate, and create your Bluetooth connection properly. You should read this documentation on the Activity lifecycle.

Community
  • 1
  • 1
Cat
  • 66,919
  • 24
  • 133
  • 141
  • This is only a proof of concept on my part, so there are actually not any other activities, currently. I've dropped in the subsection of my manifest file above, would you mind confirming this looks accurate? Thank you very much. – Garrett Sep 09 '12 at 20:04
  • That looks right. From where are you calling `Activities testActivities = new Activities();`? – Cat Sep 09 '12 at 20:06
  • I was calling it from the main class in this module. The only other logic in that class (or in this module at all, currently) was testing to see if Bluetooth was enabled, and if it was not, I was attempting to fire that off. I suppose the logical thing to do would be to move that logic into the connectBluetooth() method. – Garrett Sep 09 '12 at 20:11
  • Absolutely. So, use the first block of code I provided in this post (don't call the `Activities` from elsewhere), then move any checks/error handling into the `connectBluetooth()` method. – Cat Sep 09 '12 at 20:14
  • Understood! Well, from all appearances this should be working. I'm getting no errors anywhere, but it also appears that the Activity is never firing off. – Garrett Sep 09 '12 at 20:41