我有一个返回国家名称的函数 getCountry:
public String getCoutry(double longitude, double latitude){
Geocoder geocoder = new Geocoder(getBaseContext(), Locale.getDefault());
String country = "";
List<Address> address = null;
try{
address = geocoder.getFromLocation(latitude, longitude, 1);
if (address != null || address.size() != 0)
{
Address object = address.get(0);
country = object.getCountryName();
}else{
}
}catch(IOException e){
Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show();
}
return country;
}
android 21 及更高版本完美运行,但在 19 sdk 应用程序已停止,address.size 等于 0,为什么?
经度:14.8260 纬度:50.8706
日志猫:
11-20 13:04:22.885: D/EGL_emulation(2487): eglMakeCurrent: 0xb8522580: ver 2 0
11-20 13:04:22.905: D/AndroidRuntime(2487): Shutting down VM
11-20 13:04:22.905: W/dalvikvm(2487): threadid=1: thread exiting with uncaught exception (group=0xa4cf9b20)
11-20 13:04:22.905: E/AndroidRuntime(2487): FATAL EXCEPTION: main
11-20 13:04:22.905: E/AndroidRuntime(2487): Process: nusssoftware.tachonuss, PID: 2487
11-20 13:04:22.905: E/AndroidRuntime(2487): java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
11-20 13:04:22.905: E/AndroidRuntime(2487): at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
11-20 13:04:22.905: E/AndroidRuntime(2487): at java.util.ArrayList.get(ArrayList.java:308)
11-20 13:04:22.905: E/AndroidRuntime(2487): at nusssoftware.tachonuss.GpsLocation.getCoutry(GpsLocation.java:95)
11-20 13:04:22.905: E/AndroidRuntime(2487): at nusssoftware.tachonuss.GpsLocation$1.onClick(GpsLocation.java:79)
11-20 13:04:22.905: E/AndroidRuntime(2487): at android.view.View.performClick(View.java:4438)
11-20 13:04:22.905: E/AndroidRuntime(2487): at android.view.View$PerformClick.run(View.java:18422)
11-20 13:04:22.905: E/AndroidRuntime(2487): at android.os.Handler.handleCallback(Handler.java:733)
11-20 13:04:22.905: E/AndroidRuntime(2487): at android.os.Handler.dispatchMessage(Handler.java:95)
11-20 13:04:22.905: E/AndroidRuntime(2487): at android.os.Looper.loop(Looper.java:136)
11-20 13:04:22.905: E/AndroidRuntime(2487): at android.app.ActivityThread.main(ActivityThread.java:5017)
11-20 13:04:22.905: E/AndroidRuntime(2487): at java.lang.reflect.Method.invokeNative(Native Method)
11-20 13:04:22.905: E/AndroidRuntime(2487): at java.lang.reflect.Method.invoke(Method.java:515)
11-20 13:04:22.905: E/AndroidRuntime(2487): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
11-20 13:04:22.905: E/AndroidRuntime(2487): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
11-20 13:04:22.905: E/AndroidRuntime(2487): at dalvik.system.NativeStart.main(Native Method)
感谢您的回答!