2

I have a code that is supposed to return the user location but the Longitude keeps returning null and as a result my app keeps crashing. How do I prevent this?

//get the user longitude and latitude
LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE); 
Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
double longitude = location.getLongitude();
double latitude = location.getLatitude();

Geocoder geocoder;  //return meaningful data from user location
List<Address> addresses;
geocoder = new Geocoder(this, Locale.getDefault());
addresses = geocoder.getFromLocation(latitude, longitude, 1);
address = addresses.get(0).getAddressLine(0);
city = addresses.get(0).getLocality();
Tunaki
  • 132,869
  • 46
  • 340
  • 423
Blaze
  • 2,269
  • 11
  • 40
  • 82

0 Answers0