我正在尝试为使用 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
}