1

我很难将它击落到地理编码器没有提供地址,但我不知道为什么。

public void button_address (View v){
//Create Geocoder
gc = new Geocoder(this, Locale.getDefault());
//The string pulled from the app to be used  in the Geocoder
String loc = address.getText().toString();
//The list the Geocoder will fill
List<Address> add;
//Check to make sure there is a value in loc
Log.d(TAG, loc);
Log.d(TAG, "Before try/catch");
try {
    //Request Gecoder return string loc with an address for add
    add = gc.getFromLocationName(loc, 1);
    add.isEmpty();
    //Check to see if add is empty
    if(add.isEmpty()){ Log.d(TAG, "ADD is empty"); }

    double lat = add.get(0).getLatitude();
    double lon = add.get(0).getLongitude();
    Log.d(TAG, "lat:" + lat + ", long:" + lon);

} catch (IOException e) {
    Toast toast = Toast.makeText(this, "Please Try Entering Another Address",
            Toast.LENGTH_SHORT);
    toast.show();
    e.printStackTrace();

}

现在这是我的日志猫

05-24 19:33:21.309: D/MainActivity(32508): 3111 world Dr, lake Buena vista, fl 32830
05-24 19:33:21.309: D/MainActivity(32508): Before try/catch
05-24 19:33:21.329: D/MainActivity(32508): ADD is empty
05-24 19:33:21.329: D/AndroidRuntime(32508): Shutting down VM
05-24 19:33:21.329: W/dalvikvm(32508): threadid=1: thread exiting with uncaught exception (group=0x40a13300)
05-24 19:33:21.418: E/AndroidRuntime(32508): FATAL EXCEPTION: main
05-24 19:33:21.418: E/AndroidRuntime(32508): java.lang.IllegalStateException: Could not execute method of the activity
05-24 19:33:21.418: E/AndroidRuntime(32508):    at android.view.View$1.onClick(View.java:3591)
05-24 19:33:21.418: E/AndroidRuntime(32508):    at android.view.View.performClick(View.java:4084)
05-24 19:33:21.418: E/AndroidRuntime(32508):    at android.view.View$PerformClick.run(View.java:16966)
05-24 19:33:21.418: E/AndroidRuntime(32508):    at android.os.Handler.handleCallback(Handler.java:615)
05-24 19:33:21.418: E/AndroidRuntime(32508):    at android.os.Handler.dispatchMessage(Handler.java:92)
05-24 19:33:21.418: E/AndroidRuntime(32508):    at android.os.Looper.loop(Looper.java:137)
05-24 19:33:21.418: E/AndroidRuntime(32508):    at android.app.ActivityThread.main(ActivityThread.java:4745)
05-24 19:33:21.418: E/AndroidRuntime(32508):    at java.lang.reflect.Method.invokeNative(Native Method)
05-24 19:33:21.418: E/AndroidRuntime(32508):    at java.lang.reflect.Method.invoke(Method.java:511)
05-24 19:33:21.418: E/AndroidRuntime(32508):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
05-24 19:33:21.418: E/AndroidRuntime(32508):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
05-24 19:33:21.418: E/AndroidRuntime(32508):    at dalvik.system.NativeStart.main(Native Method)
05-24 19:33:21.418: E/AndroidRuntime(32508): Caused by: java.lang.reflect.InvocationTargetException
05-24 19:33:21.418: E/AndroidRuntime(32508):    at java.lang.reflect.Method.invokeNative(Native Method)
05-24 19:33:21.418: E/AndroidRuntime(32508):    at java.lang.reflect.Method.invoke(Method.java:511)
05-24 19:33:21.418: E/AndroidRuntime(32508):    at android.view.View$1.onClick(View.java:3586)
05-24 19:33:21.418: E/AndroidRuntime(32508):    ... 11 more
05-24 19:33:21.418: E/AndroidRuntime(32508): Caused by: java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
05-24 19:33:21.418: E/AndroidRuntime(32508):    at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:251)
05-24 19:33:21.418: E/AndroidRuntime(32508):    at java.util.ArrayList.get(ArrayList.java:304)
05-24 19:33:21.418: E/AndroidRuntime(32508):    ... 14 more
05-24 19:33:23.839: I/Process(32508): Sending signal. PID: 32508 SIG: 9

因此,基于 add 返回空的事实,然后在 logcat 中我得到: IndexOutOfBoundsException: Invalid index 0, size is 0 我确信地理编码器以某种方式失败了。我在清单中有互联网权限,并检查以确保互联网正在通过网络浏览器在模拟器上运行。

希望其他人可以看到我错过的简单问题或有建议。提前感谢您提供的所有帮助。

4

1 回答 1

1

您可以检查 isEmpty 但仍尝试从下一行的列表中获取元素。

try {
    //Request Gecoder return string loc with an address for add
    add = gc.getFromLocationName(loc, 1);
    add.isEmpty();
    //Check to see if add is empty
    if(add.isEmpty()){ Log.d(TAG, "ADD is empty"); }
    else {
    double lat = add.get(0).getLatitude();
    double lon = add.get(0).getLongitude();
    Log.d(TAG, "lat:" + lat + ", long:" + lon);
    }
} catch (IOException e) {
    Toast toast = Toast.makeText(this, "Please Try Entering Another Address",
            Toast.LENGTH_SHORT);
    toast.show();
    e.printStackTrace();

}
于 2013-05-24T10:59:52.400 回答