0

tried to get the address using latitude and longitude. Same code works on other devices though, address list returns null on some devices(physical) android version: 11.

MainActivity.java

public void onLocationResult(@NonNull LocationResult locationResult) {
                                    super.onLocationResult(locationResult);

                                    LocationServices.getFusedLocationProviderClient(MainActivity.this)
                                            .removeLocationUpdates(this);

                                    if (locationResult != null && locationResult.getLocations().size() >0){

                                        int index = locationResult.getLocations().size() - 1;
                                        double latitude = locationResult.getLocations().get(index).getLatitude();
                                        double longitude = locationResult.getLocations().get(index).getLongitude();


                                        geocoder = new Geocoder(MainActivity.this, Locale.getDefault());

                                        try {
                                            addresses = geocoder.getFromLocation(latitude, longitude, 1); // Here 1 represent max location result to returned, by documents it recommended 1 to 5
                                        } catch (IOException e) {
                                            e.printStackTrace();
                                        }

                                        String address = addresses.get(0).getAddressLine(0);
                                        String city = addresses.get(0).getLocality();
                                        String state = addresses.get(0).getAdminArea();
                                        String country = addresses.get(0).getCountryName();


                                        AddressText.setText("address: "+ address + "\n" + "city: "+ city + "state: "+ state + "\n" + "country: "+ country);
                                    }
                                }

Error:

2022-05-25 09:36:53.805 29773-29773/com.example.location E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.location, PID: 29773
    java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
        at java.util.ArrayList.get(ArrayList.java:437)
        at com.example.location.MainActivity$2.onLocationResult(MainActivity.java:142)
        at com.google.android.gms.internal.location.zzap.notifyListener(com.google.android.gms:play-services-location@@18.0.0:2)
        at com.google.android.gms.common.api.internal.ListenerHolder.zaa(com.google.android.gms:play-services-base@@18.0.1:2)
        at com.google.android.gms.common.api.internal.zacb.run(Unknown Source:4)
        at android.os.Handler.handleCallback(Handler.java:938)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:233)
        at android.app.ActivityThread.main(ActivityThread.java:8010)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:631)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:978)

1 Answers1

0

Geocoder is not stable and sometimes stops working, when Geocoder breaks it simply returns an empty list. Unfortunately, there is no way to check if Geocoder is working in advance. The workaround is to connect to the Google Maps web api when your Geocoder returns an empty list. Look at this post for an example of the code.

Sreehari K
  • 887
  • 6
  • 16
  • Thanks!, also the location which I'm getting is the last location any app requested(google maps), if the google maps crashed or not being loaded, this app doesn't get the location. why does this happen? – GIRIDHAR B May 25 '22 at 05:51