Google Play Services Location throwing NullPointerException
I am trying to run a service that tracks a users location using the Google
Play Location Services. I've followed the [Android Developers Google Play]
(http://developer.android.com/google/play-services/setup.html)
instructions but currently have a crashing app.
I am using the android emulator AVD with a Google API target (17).
I have
1) Installed the Google Play Services SDK
2) Made a copy of the Google Play services library project, imported it
into my Eclipse workspace, and referenced it in my current app, using
these [instructions]
(http://developer.android.com/tools/projects/projects-eclipse.html#ReferencingLibraryProject)
I am not sure whether or not I have to add anything to the Manifest
Some background on my app structure: I have created a Location Services
class with all of the error handling, and am calling that class' methods
via a service. My app is currently crashing when I call the:
GooglePlayServicesUtil.isGooglePlayServicesAvailable(mContext);
command which is in the servicesConnected() which is the first instance I
call anything Google Play related. So basically, where I am at: I am
trying to call the method checking if Google Play services is available,
and the app is crashing on:
08-19 03:51:19.464: E/AndroidRuntime(1112): java.lang.RuntimeException:
Unable to start service
com.scarr025.zwilio.AdventureService@40d24920 with Intent { flg=0x4
cmp=com.scarr025.zwilio/.AdventureService (has extras) }:
java.lang.NullPointerException
Please help!!
Code is below:
Google Play Services Class
public class AdventureLocator extends FragmentActivity implements
GooglePlayServicesClient.ConnectionCallbacks,
GooglePlayServicesClient.OnConnectionFailedListener {
protected void onCreate(Bundle savedInstanceState) {
mLocClient = new LocationClient(this, this, this);
mContext = this;
}
public void startConnection() {
mLocClient.connect();
}
public Location getLastLocation() {
return mLocClient.getLastLocation();
}
// Method that encapsulates the check for Google Play services
public boolean servicesConnected() {
// Check that Google Play services is available
int resultCode =
GooglePlayServicesUtil.isGooglePlayServicesAvailable(mContext);
// If Google Play services is available
if (ConnectionResult.SUCCESS == resultCode) {
// In debug mode, log the status
Log.d("$$AdventureLocator$$", "In servicesConnected; Google Play
services is available (returned true).");
// Continue
return true;
// Google Play services was not available for some reason
} else {
// Get the error code
// Get the error dialog from Google Play services
Dialog errorDialog = GooglePlayServicesUtil.getErrorDialog(
resultCode,
this,
CONNECTION_FAILURE_RESOLUTION_REQUEST);
// If Google Play services can provide an error dialog
if (errorDialog != null) {
// Create a new DialogFragment for the error dialog
ErrorDialogFragment errorFragment = new ErrorDialogFragment();
// Set the dialog in the DialogFragment
errorFragment.setDialog(errorDialog);
// Show the error dialog in the DialogFragment
errorFragment.show(getSupportFragmentManager(), "Location
Updates");
}
return false;
}
...
Manifest File
...
<service
android:name=".AdventureService"
android:label="@string/title_adventure_service" >
</service>
<activity
android:name=".AdventureLocator"
android:label="@string/title_adventure_locator" >
</activity>
</application>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
No comments:
Post a Comment