嗨,大家好我與地理編碼器工作異常,通過獲取的緯度和經度,但我的代碼工作做好,有時否則這是的GeoCoder cuauses java.lang.IndexOutOfBoundsException:無效指數3,大小爲3個
java.lang.IndexOutOfBoundsException: Invalid index 3, size is 3
at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:251)
at java.util.ArrayList.get(ArrayList.java:304)
at com.example.mine4.latitudelongitudeconverter.LatLangToLoCaTion$1.onClick(LatLangToLoCaTion.java:70)
at android.view.View.performClick(View.java:4222)
at android.view.View$PerformClick.run(View.java:17620)
at android.os.Handler.handleCallback(Handler.java:800)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5391)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
at dalvik.system.NativeStart.main(Native Method)
final int maxResult =5;
String addressList[] = new String[maxResult];
try {
List<Address> addresses = geocoder.getFromLocation(LATITUDE, LONGITUDE, maxResult);
if(addresses != null) {
for (int j=0; j<maxResult; j++){
Address returnedAddress = addresses.get(j);
StringBuilder strReturnedAddress = new StringBuilder();
for(int i=0; i<returnedAddress.getMaxAddressLineIndex(); i++) {
strReturnedAddress.append(returnedAddress.getAddressLine(i)).append("\n");
}
addressList[j] = strReturnedAddress.toString();
}
adapter = new ArrayAdapter<>(getApplicationContext(),
android.R.layout.simple_spinner_item, addressList);
adapter.setDropDownViewResource(android.R.layout.
simple_spinner_dropdown_item);
myAddressList.setAdapter(adapter);
myAddressList.setOnItemSelectedListener(myAddressListOnItemSelectedListener);
}
現在請讓我找出真正的問題。這裏有一兩件事我想告訴你,我從用戶提前獲得經緯度通過的EditText 感謝
我想'maxResult'應該等於'addresses.size(打破)',而不是5 – Hacketo