【发布时间】:2017-01-18 09:10:00
【问题描述】:
我遇到了 this question 的问题,我也对 lat 和 lon 进行了相同的硬编码。
我得到异常确认大小为
Exception for the locationjava.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
当我在 Redmi a4 上尝试相同的代码时,它运行良好,大小为 1,地理编码也运行良好,将所有地址返回给我。当我在联想上运行相同的代码时,它给出了异常。我检查了纬度和经度,它们的值在我追踪时指向我的位置。那么我应该怎么做才能让应用程序在两部手机上运行。 运行 Android 4.4.4 的联想手机 红米运行 6.0.0
if(lat != null && lon !=null) {
try{
Geocoder geocoder;
List<Address> addresses;
geocoder = new Geocoder(this, Locale.getDefault());
addresses = geocoder.getFromLocation(dlat, dlon, 1); // Here 1 represent max location result to returned, by documents it recommended 1 to 5
System.out.println("this is the size of address size"+addresses.size());
String address = addresses.get(0).getAddressLine(0); // If any additional address line present than only, check with max available address lines by getMaxAddressLineIndex()
String city = addresses.get(0).getLocality();
String state = addresses.get(0).getAdminArea();
String country = addresses.get(0).getCountryName();
String postalCode = addresses.get(0).getPostalCode();
String knownName = addresses.get(0).getFeatureName();
complete_address=address + "_" + city + "_" + address + "_" + postalCode + "_" + knownName + "_" + country;
txtOutputLat.setText(complete_address);
txtOutputLon.setText("Map Address");
}
catch(Exception e)
{
System.out.println("Exception for the location"+e.toString());
}
}
【问题讨论】:
-
你的纬度长得正确吗?
-
是的,在我的应用程序中,当我单击按钮时,它会打开 waze 上的 lat lan,它工作正常。此外,当我在网上追踪它们时,它会显示我所在的位置。
标签: android location google-geocoder