Reputation: 31
I am trying to get real-time location updates using the GoogleAPIClient
. While GoogleAPIClient
is connected but the location object is null and it complains that location.getProvider()
is null. Can someone please help?
This is my code :
public void onConnected(Bundle connectionHint) {
Log.i(TAG, "Connected to GoogleApiClient " + myGoogleApiClient.isConnected());
myCurrentLocation = LocationServices.FusedLocationApi.getLastLocation(myGoogleApiClient);
// Debug - At this point myCurrentLocation is NULL
if (myCurrentLocation == null) {
Log.i(TAG, "myCurrentLocation is : " + myCurrentLocation);
// Debug - myCurrentLocation is STILL NULL
LocationServices.FusedLocationApi.requestLocationUpdates(
myGoogleApiClient, myLocationRequest, this);
// Debug - app crashes with stack trace below at this point
Log.i(TAG, "Doesnt't reach here : " + myCurrentLocation);
}
myLastUpdateTime = DateFormat.getTimeInstance().format(new Date());
updateUILayout();
}
protected synchronized void buildGoogleApiClient() {
Log.i(TAG, "Building GoogleApiClient");
myGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
Log.i(TAG, "myGoogleApiClient is :" + myGoogleApiClient.isConnected());
createLocationRequest();
}
protected void createLocationRequest() {
myLocationRequest = new LocationRequest();
myLocationRequest.setInterval(10000);
myLocationRequest.setFastestInterval(5000);
myLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
}
The main problem after debugging I found was with:
LocationServices.FusedLocationApi.requestLocationUpdates(
myGoogleApiClient, myLocationRequest, this);
This is not setting myCurrentLocation. It's always NULL. And in the ADB logcat says:
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.location.Location.getProvider()' on a null object reference
at com.adrenalin.myfirstapp.MyActivity.onConnected(MyActivity.java:130)
at com.google.android.gms.internal.jm.f(Unknown Source)
at com.google.android.gms.common.api.c.gJ(Unknown Source)
at com.google.android.gms.common.api.c.d(Unknown Source)
at com.google.android.gms.common.api.c$2.onConnected(Unknown Source)
at com.google.android.gms.internal.jm.f(Unknown Source)
at com.google.android.gms.internal.jm.dU(Unknown Source)
at com.google.android.gms.internal.jl$h.b(Unknown Source)
Upvotes: 1
Views: 9574
Reputation: 11
In my case I'm using a fragment class, is there any workaround for using this on LocationServices.FusedLocationApi.requestLocationUpdates()?
@Override
public void onLocationChanged(Location location) {
mLastLocation = location;
displayLocation();
}
private void startLocationUpdates() {
if(ActivityCompat.checkSelfPermission( getActivity().getApplicationContext(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED &&
ActivityCompat.checkSelfPermission( getActivity().getApplicationContext(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED )
{
return;
}
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient,mLastLocation,this);
}
Upvotes: 1
Reputation: 491
Use following method if you are getting Null localion
if (myCurrentLocation == null) {
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
} else {
//your location code here...
}
And override following method for retrying for location
@Override
public void onLocationChanged(Location location) {
//your location code here...
}
Upvotes: 0
Reputation: 6394
When you are implementing
LocationServices.FusedLocationApi.requestLocationUpdates(
myGoogleApiClient, myLocationRequest, this);
you need to override the callback for onLocationChanged
so that it knows what to do with the location as it updates.
Try this:
@Override
public void onLocationChanged(Location location) {
myCurrentLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
}
Upvotes: 1