To be clear: This is an activity in my Android application that is meant to pull the coordinates for the users location using the GPS_PROVIDER. The Activity contains a button which, when pressed, should initiate a method that obtains the coordinate data. The problem is that the application crashes when there is no previously known location information (ie if the phone was recently reset). If I open up the Maps application (for example) and pinpoint my location, then re-open my own application and run this method, it works as intended. My question is why is this crashing and/or how can I prevent this crash from occurring? Help is appreciated, thanks.
This method is run when the button is pressed – and an intent response is generated back to the calling activity when the coordinates are properly found:
protected void getCurrentLocation() {
Location location = null;
try {
location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
} catch (IllegalArgumentException iae) { }
if (location != null) {
longV = location.getLongitude();
latV = location.getLatitude();
response(longV, latV);
} else {
getCurrentLocation();
}
I’m guessing that you are receiving a StackOverflowException, because if
locationisnullyou call the exact same function creating an indefinite loop…If there is no last know location, you need to request a new location. (
getLastKnownLocation()will not change on its own no matter how many times you call it.)