0

based on this tutorial: http://www.androidhive.info/2012/07/android-gps-location-manager-tutorial/. I can now get latitude and longtitude based on location. Now I', trying to get the exact address with Geocoder: (Main is the class code above belongs to)

GPSTracker GPS = new GPSTracker(Main.this);

double latitude = GPS.getLatitude();
double longitude = GPS.getLongitude();

Geocoder geocoder;
List<Address> addresses;
geocoder = new Geocoder(Main.this, Locale.getDefault());
addresses = geocoder.getFromLocation(latitude, longitude, 1);
String address = addresses.get(0).getAddressLine(0);

LogCat says:

07-06 11:29:41.911: W/System.err(1670): java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0

I wonder what am I doing wrong?

Hendra Anggrian
  • 5,780
  • 13
  • 57
  • 97

1 Answers1

0
GPSTracker GPS = new GPSTracker(Main.this);

double latitude = GPS.getLatitude();
double longitude = GPS.getLongitude();

Geocoder geocoder;
List<Address> addresses;
geocoder = new Geocoder(Main.this, Locale.getDefault());

addresses = geocoder.getFromLocation(latitude, longitude, 1);
if(addresses!=null && addresses.size()!=0){

    String address = addresses.get(0).getAddressLine(0);

}

//if(addresses!=null && addresses.size()!=0)...check this portion..your address cannot be null and and the the size of array should not be zero...

  • if(addresses!=null && addresses.size()!=0)...check this portion..your address cannot be null and and the the size of array should not be zero... – sazzad jesan Mar 05 '18 at 07:13