How to prevent app from closing when Geocoder address list is null?

linus

I am trying to handle errors for my application that uses Geocoder, but whenever i try to get the coordinates of a non-existent location my app crashes. I know this has been asked before but i don't think it was resolved. Anyway i can prevent it from force closing, and just notify the user to input a correct string?

Here is my code. a Sample string would be "qwihedhladhaw"

getCoordinatesFromString("qwihedhladhaw");

    public double[] getCoordinatesFromString (String location) {
    Utils util = new Utils();
    double coordinates[] = {0.0, 0.0};
    if(util.checkIfEmptyString(location)) {
        this.canGetCoordinates = false;
        System.out.println("EMPTY STRINGS");
    } else {
        Geocoder geocoder = new Geocoder(mContext);

        //geocoding
        List<Address> addressList;
        try {
            addressList = geocoder.getFromLocationName(location, 1, 14.479894, 120.970062, 14.774883, 121.061676 );
            Address address = addressList.get(0);
            System.out.println(address);
            if(address.hasLatitude() && address.hasLongitude()){
                coordinates[0] = address.getLatitude();
                coordinates[1] = address.getLongitude();
                this.canGetCoordinates = true;
                this.isLocationValid = true;
                System.out.println(coordinates[0]);
                System.out.println(coordinates[1]);
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } 
    }
    return coordinates;
}

Output logs are:

    {07-30 23:12:50.291: I/System.out(26237): sDestination: 
    07-30 23:12:50.291: I/System.out(26237): sStart: qwert
    07-30 23:12:50.526: D/AndroidRuntime(26237): Shutting down VM
    07-30 23:12:50.531: W/dalvikvm(26237): threadid=1: thread exiting with uncaught exception (group=0x40c4a930)
    07-30 23:12:50.566: E/AndroidRuntime(26237): FATAL EXCEPTION: main
    07-30 23:12:50.566: E/AndroidRuntime(26237): java.lang.IllegalStateException: Could not execute method of the activity
    07-30 23:12:50.566: E/AndroidRuntime(26237):    at android.view.View$1.onClick(View.java:3599)
    07-30 23:12:50.566: E/AndroidRuntime(26237):    at android.view.View.performClick(View.java:4204)
    07-30 23:12:50.566: E/AndroidRuntime(26237):    at android.view.View$PerformClick.run(View.java:17360)
    07-30 23:12:50.566: E/AndroidRuntime(26237):    at android.os.Handler.handleCallback(Handler.java:725)
    07-30 23:12:50.566: E/AndroidRuntime(26237):    at android.os.Handler.dispatchMessage(Handler.java:92)
    07-30 23:12:50.566: E/AndroidRuntime(26237):    at android.os.Looper.loop(Looper.java:137)
    07-30 23:12:50.566: E/AndroidRuntime(26237):    at android.app.ActivityThread.main(ActivityThread.java:5233)
    07-30 23:12:50.566: E/AndroidRuntime(26237):    at java.lang.reflect.Method.invokeNative(Native Method)
    07-30 23:12:50.566: E/AndroidRuntime(26237):    at java.lang.reflect.Method.invoke(Method.java:511)
    07-30 23:12:50.566: E/AndroidRuntime(26237):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:799)
    07-30 23:12:50.566: E/AndroidRuntime(26237):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:566)
    07-30 23:12:50.566: E/AndroidRuntime(26237):    at dalvik.system.NativeStart.main(Native Method)
    07-30 23:12:50.566: E/AndroidRuntime(26237): Caused by: java.lang.reflect.InvocationTargetException
    07-30 23:12:50.566: E/AndroidRuntime(26237):    at java.lang.reflect.Method.invokeNative(Native Method)
    07-30 23:12:50.566: E/AndroidRuntime(26237):    at java.lang.reflect.Method.invoke(Method.java:511)
    07-30 23:12:50.566: E/AndroidRuntime(26237):    at android.view.View$1.onClick(View.java:3594)
    07-30 23:12:50.566: E/AndroidRuntime(26237):    ... 11 more
    07-30 23:12:50.566: E/AndroidRuntime(26237): Caused by: java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
    07-30 23:12:50.566: E/AndroidRuntime(26237):    at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:251)
    07-30 23:12:50.566: E/AndroidRuntime(26237):    at java.util.ArrayList.get(ArrayList.java:304)
    07-30 23:12:50.566: E/AndroidRuntime(26237):    at com.example.otpxmlgetter.ReverseGeocode.getCoordinatesFromString(ReverseGeocode.java:33)
    07-30 23:12:50.566: E/AndroidRuntime(26237):    at com.example.otpxmlgetter.MainActivity.planTrip(MainActivity.java:136)
    07-30 23:12:50.566: E/AndroidRuntime(26237):    ... 14 more
    07-30 23:17:50.721: I/Process(26237): Sending signal. PID: 26237 SIG: 9
}
malimo
Address address = addressList.get(0);

is your problem. if the location is non-existent the addressList will be empty, so you get an java.lang.IndexOutOfBoundsException when trying to access the first listItem.

simply check if your list has items:

if(addressList != null && addressList.size() > 0) {
    // location exists
    Address address = addressList.get(0);
    System.out.println(address);
    if(address.hasLatitude() && address.hasLongitude()){
        coordinates[0] = address.getLatitude();
        coordinates[1] = address.getLongitude();
        this.canGetCoordinates = true;
        this.isLocationValid = true;
        System.out.println(coordinates[0]);
        System.out.println(coordinates[1]);
    }
} else { 
    // location does not exist
}

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

CDockablePane prevent from closing

분류에서Dev

Prevent GNU Screen from closing

분류에서Dev

Prevent Modal Dialog from closing on submit when client side validation fails

분류에서Dev

Prevent typeahead dataset from closing on click

분류에서Dev

Prevent file handles from closing at end of execution

분류에서Dev

How to create a filter to prevent action method from finding null objects

분류에서Dev

Google map (geocoder.geocode) breaks the markers list while using address instead of LatLng

분류에서Dev

Forwarding with postfix: prevent rewriting of from address

분류에서Dev

How to select only not null objects from list

분류에서Dev

Prevent AngularJS modal from closing based on logic inside the modal controller

분류에서Dev

How to prevent images from shifting content when loading?

분류에서Dev

How do I prevent a script from terminating when the shell exits?

분류에서Dev

Reading from a binary file when closing and opening it

분류에서Dev

How to reopen/prevent closing of ControlsFX LoginDialog on failed login?

분류에서Dev

How can I prevent the source file is different from when the module was built when using Resharper

분류에서Dev

Prevent Smart app banner from showing on iPad

분류에서Dev

Titanium - prevent exitOnClose from stopping the app

분류에서Dev

Prevent jquery colorbox closing on postback

분류에서Dev

Why does bash convert .* to hidden file list in current dir and how to prevent it from doing it?

분류에서Dev

how to prevent TouchEvent from scrolling

분류에서Dev

Stop laptop from suspending when closing lid in lightdm

분류에서Dev

How to prevent a password or other sensitive information from being stored in the bash history when using grep?

분류에서Dev

How to prevent divs from moving when I hover on an element that changes size above it

분류에서Dev

How to prevent from bypassing registration form when the user's inputs are incorrect?

분류에서Dev

How can I prevent Windows from overwriting GRUB when using a dual-boot machine

분류에서Dev

Prevent GridView from resizing when clicking on The LinkButton

분류에서Dev

How to know the IP address of a Mac when it is in network

분류에서Dev

How to prevent screen locking when lid is closed?

분류에서Dev

How to Prevent Dropbox Access Elsewhere when Symlink?

Related 관련 기사

  1. 1

    CDockablePane prevent from closing

  2. 2

    Prevent GNU Screen from closing

  3. 3

    Prevent Modal Dialog from closing on submit when client side validation fails

  4. 4

    Prevent typeahead dataset from closing on click

  5. 5

    Prevent file handles from closing at end of execution

  6. 6

    How to create a filter to prevent action method from finding null objects

  7. 7

    Google map (geocoder.geocode) breaks the markers list while using address instead of LatLng

  8. 8

    Forwarding with postfix: prevent rewriting of from address

  9. 9

    How to select only not null objects from list

  10. 10

    Prevent AngularJS modal from closing based on logic inside the modal controller

  11. 11

    How to prevent images from shifting content when loading?

  12. 12

    How do I prevent a script from terminating when the shell exits?

  13. 13

    Reading from a binary file when closing and opening it

  14. 14

    How to reopen/prevent closing of ControlsFX LoginDialog on failed login?

  15. 15

    How can I prevent the source file is different from when the module was built when using Resharper

  16. 16

    Prevent Smart app banner from showing on iPad

  17. 17

    Titanium - prevent exitOnClose from stopping the app

  18. 18

    Prevent jquery colorbox closing on postback

  19. 19

    Why does bash convert .* to hidden file list in current dir and how to prevent it from doing it?

  20. 20

    how to prevent TouchEvent from scrolling

  21. 21

    Stop laptop from suspending when closing lid in lightdm

  22. 22

    How to prevent a password or other sensitive information from being stored in the bash history when using grep?

  23. 23

    How to prevent divs from moving when I hover on an element that changes size above it

  24. 24

    How to prevent from bypassing registration form when the user's inputs are incorrect?

  25. 25

    How can I prevent Windows from overwriting GRUB when using a dual-boot machine

  26. 26

    Prevent GridView from resizing when clicking on The LinkButton

  27. 27

    How to know the IP address of a Mac when it is in network

  28. 28

    How to prevent screen locking when lid is closed?

  29. 29

    How to Prevent Dropbox Access Elsewhere when Symlink?

뜨겁다태그

보관