2015-07-10 70 views
-1

嗨,大家好我與地理編碼器工作異常,通過獲取的緯度和經度,但我的代碼工作做好,有時否則這是的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)
現在我不能understand.The代碼,我使用的這個問題是

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 感謝

+1

我想'maxResult'應該等於'addresses.size(打破)',而不是5 – Hacketo

回答

0

這意味着你正在傳遞無效index 3,當size3,基本上指數從0開始的,所以你可以能通0,1,2索引從數組或列表中獲取值,當其大小爲3.現在,當您傳遞索引3時,表示IndexOutOfBoundsException,您無法將索引超過其大小。要獲得3個索引值ArrayList大小應該是4或更多。

0

出於某種原因,你Ĵ變成3,大小爲3,因此它在裏面的for循環

相關問題