Geocoder not putting out Address

梦想与她 提交于 2020-01-05 10:08:43

问题


I have trouble shot it down to the Geocoder not putting out an address, but I do not know why.

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();

}

Now here is my logcat

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

So based on the fact that add returns empty and then in the logcat I get: IndexOutOfBoundsException: Invalid index 0, size is 0 I am sure that somehow the Geocoder is failing. I have internet permissions in the manifest, and checked to make sure that internet is working on the emulator through a web browser.

Hopefully some one else can see a simple problem that I have missed or has a recommendation. Thanks in advance for all you help.


回答1:


You can check for isEmpty but still trying to get the element from the list in next line.

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();

}


来源:https://stackoverflow.com/questions/16733270/geocoder-not-putting-out-address

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!