gpt4 book ai didi

android - 无法在android中使用地理编码器获取地址

转载 作者:行者123 更新时间:2023-11-30 02:56:51 26 4
gpt4 key购买 nike

  • 我正在尝试使用纬度和经度获取位置详细信息
  • 我正在使用模拟器运行这段代码,因为我没有设备,当我运行代码时,出现日志中显示的错误

MainActivity.java

public class MainActivity extends Activity {
double LATITUDE = 37.42233;
double LONGITUDE = -122.083;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

Geocoder geocoder = new Geocoder(this, Locale.ENGLISH);

try {
List<Address> addresses = geocoder.getFromLocation(LATITUDE, LONGITUDE, 1);

if(addresses != null) {
Address returnedAddress = addresses.get(0);
StringBuilder strReturnedAddress = new StringBuilder("Address:\n");
for(int i=0; i<returnedAddress.getMaxAddressLineIndex(); i++) {
strReturnedAddress.append(returnedAddress.getAddressLine(i)).append("\n");
}

Log.d("RESULT",strReturnedAddress.toString());
}
else{
Log.d("NO-RESULT","NO-RESULT");
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Log.d("NO-RESULT","NO-RESULT");
}

}

}

日志:

04-17 12:50:22.489: E/AndroidRuntime(609): FATAL EXCEPTION: main
04-17 12:50:22.489: E/AndroidRuntime(609): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.findingpresentlocation/com.example.findingpresentlocation.MainActivity}: java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
04-17 12:50:22.489: E/AndroidRuntime(609): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1955)
04-17 12:50:22.489: E/AndroidRuntime(609): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
04-17 12:50:22.489: E/AndroidRuntime(609): at android.app.ActivityThread.access$600(ActivityThread.java:122)
04-17 12:50:22.489: E/AndroidRuntime(609): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
04-17 12:50:22.489: E/AndroidRuntime(609): at android.os.Handler.dispatchMessage(Handler.java:99)
04-17 12:50:22.489: E/AndroidRuntime(609): at android.os.Looper.loop(Looper.java:137)
04-17 12:50:22.489: E/AndroidRuntime(609): at android.app.ActivityThread.main(ActivityThread.java:4340)
04-17 12:50:22.489: E/AndroidRuntime(609): at java.lang.reflect.Method.invokeNative(Native Method)
04-17 12:50:22.489: E/AndroidRuntime(609): at java.lang.reflect.Method.invoke(Method.java:511)
04-17 12:50:22.489: E/AndroidRuntime(609): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
04-17 12:50:22.489: E/AndroidRuntime(609): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
04-17 12:50:22.489: E/AndroidRuntime(609): at dalvik.system.NativeStart.main(Native Method)
04-17 12:50:22.489: E/AndroidRuntime(609): Caused by: java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
04-17 12:50:22.489: E/AndroidRuntime(609): at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:251)
04-17 12:50:22.489: E/AndroidRuntime(609): at java.util.ArrayList.get(ArrayList.java:304)
04-17 12:50:22.489: E/AndroidRuntime(609): at com.example.findingpresentlocation.MainActivity.onCreate(MainActivity.java:30)
04-17 12:50:22.489: E/AndroidRuntime(609): at android.app.Activity.performCreate(Activity.java:4465)
04-17 12:50:22.489: E/AndroidRuntime(609): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
04-17 12:50:22.489: E/AndroidRuntime(609): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1919)
04-17 12:50:22.489: E/AndroidRuntime(609): ... 11 more

最佳答案

您只是检查返回的列表是否为空,但它可能是空的。你可以在documentation中看到返回的列表可以为 null 或为空,因此还要检查大小:

if (addresses != null && !addresses.isEmpty())

此外,您不应该在主线程中执行此操作,例如使用一个异步任务。为什么解释here :

The method is synchronous, and may take a long time to do its work, so you should call the method from the doInBackground() method of an AsyncTask.

关于android - 无法在android中使用地理编码器获取地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23127050/

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