gpt4 book ai didi

android - 地理编码器不显示地址

转载 作者:行者123 更新时间:2023-11-30 03:35:08 25 4
gpt4 key购买 nike

我无法将其发送给 Geocoder,因为它没有提供地址,但我不知道为什么。

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

因此,基于添加返回空的事实,然后在 logcat 中我得到:IndexOutOfBoundsException: Invalid index 0, size is 0我确信 Geocoder 以某种方式失败了。我在 list 中拥有互联网权限,并通过网络浏览器检查以确保互联网在模拟器上正常工作。

希望其他人可以看到我遗漏的简单问题或提出建议。在此先感谢您提供的所有帮助。

最佳答案

您可以检查 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();

}

关于android - 地理编码器不显示地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16733270/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com