0

我正在尝试为使用 Geocoder 的应用程序处理错误,但是每当我尝试获取不存在位置的坐标时,我的应用程序就会崩溃。我知道以前有人问这个问题, 但我认为没有解决。无论如何,我可以阻止它强制关闭,并通知用户输入正确的字符串?

这是我的代码。示例字符串为“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;
}

输出日志是:

    {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
}
4

2 回答 2

0

你总是可以捕捉到这个 IllegalStateException。如果方法没有声明抛出它,则捕获异常并检查它是否是 IllegalStateException 的实例。否则,重新抛出异常。

于 2013-07-30T15:40:31.600 回答
0
Address address = addressList.get(0);

是你的问题。如果该位置不存在,则 addressList 将为空,因此您java.lang.IndexOutOfBoundsException在尝试访问第一个 listItem 时会得到一个。

只需检查您的列表是否有项目:

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
}
于 2013-07-30T15:40:34.710 回答