My App uses LocationServices to request location updates. It sends the results via a PendingIntent to a Receiver. In onReceive() a LocationResult is extracted from the intent, which yields a Location that is then expressed in terms of Lat and Lng, to be used further. Sounds OK, right?
Well, the App started to crash with NullPointerException.
I investigated using Debugger and found that the three lines of code which I described above executed fine, but then, instead of proceeding to the following lines of code, execution was somehow directed back again to the first of the three lines, which extracts a LocationResult from an intent. This now turned out to be null, and crashed on the following line with NullPointerException, as LocationResult was assumed not to be null.
I have no idea why this is happening. Any pointers, please? (Sorry for the pun)
Here is my code:
public class MyLocationUpdateReceiver extends BroadcastReceiver {
private static final String TAG = "MyLocationUpdateRcvr";
// empty constructor
public MyLocationUpdateReceiver(){}
@Override
public void onReceive(Context context, Intent intent) {
Log.i(TAG,"onReceive update");
// extract locationResult from intent received from Update Request
LocationResult locationResult = LocationResult.extractResult(intent);
double locationLat = locationResult.getLastLocation().getLatitude();
double locationLng = locationResult.getLastLocation().getLongitude();
// send the Lat and Lng of new location to MapActivity via intent
Intent mIntent = new Intent(context.getApplicationContext(),
MapActivity.class);
mIntent.putExtra("New_Lat", locationLat);
mIntent.putExtra("New_Lng", locationLng);
mIntent.addFlags(FLAG_ACTIVITY_NEW_TASK);
context.startActivity(mIntent);
}
}
And here is the screenshot of the first time through.Note the LocationResult is not null.
Still on the first time round...
and on the second time round...


