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
}
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] 삭제
몇 마디 만하겠습니다