2013-06-08 2 views
0

2이 코드는 약간의 도움이 필요합니다. IndexOutOfBoundsException이 발생합니다. 나는 이유를 알지만 나는 그것을 고치려고 애써왔다. 문제 : eMain.getElementsByClass ("trA1"). size() = 8 및 eMain.getElementsByClass ("trA2") size() = 7 (크기는 많이 있지만 trA1은 항상 trA2보다 큽니다. add (trA1), add (trA2), add (trA1), add (trA2) ...... (for 루프를 사용하고 있습니다.)하지만 각 요소를 ArrayList에 추가해야합니다. 요소 8, trA2 요소가 8 없기 때문에 예외가 발생합니다. 요소가 null 또는 ""있는지 확인하기 위해 If 문을 시도했지만 예외가 여전히 발생합니다. 어떤 아이디어.Arraylist에 요소를 추가 할 때 문제가 발생합니다. JSoup Android

   // game list data 
      if (!eMain.equals("")) { 
       for (int i=0; i < eMain.getElementsByClass("trA1").size(); i++) { 
        // check if AsyncTask was cancelled 
        if (isCancelled()) 
         break; 

        if (eMain.getElementsByClass("trA1").select("strong").get(i).text() != null) { 
         // get string data 
         String titleA1 = eMain.getElementsByClass("trA1").select("strong").get(i).text(); 
         String achAmountA1 = eMain.getElementsByClass("trA1").select("td[align]").get(mAchAmountCounter).text(); 
         String gsAmountA1 = eMain.getElementsByClass("trA1").select("td[align]").get(mGsAmountCounter).text(); 
         String aPageLinkA1 = eMain.getElementsByClass("trA1").select("td a").get(mPageLinkCounter).attr("abs:href"); 
         String iconSrcA1 = eMain.getElementsByClass("trA1").select("td a img").get(i).attr("abs:src"); 

         // create game object 
         GameObject gameObjectA1 = new GameObject(); 
         gameObjectA1.setGlTitle(titleA1); 
         gameObjectA1.setGlAchAmount(achAmountA1 + " achievements"); 
         gameObjectA1.setGlGSAmount(gsAmountA1 + " gamer score"); 
         gameObjectA1.setGlAchPageUrl(aPageLinkA1); 
         gameObjectA1.setGlIcon(iconSrcA1); 
         mGameObjectList.add(gameObjectA1); 
        } 

        if (eMain.getElementsByClass("trA2").select("strong").get(i).text() != null) { 
         String titleA2 = eMain.getElementsByClass("trA2").select("strong").get(i).text(); 
         String achAmountA2 = eMain.getElementsByClass("trA2").select("td[align]").get(mAchAmountCounter).text(); 
         String gsAmountA2 = eMain.getElementsByClass("trA2").select("td[align]").get(mGsAmountCounter).text(); 
         String aPageLinkA2 = eMain.getElementsByClass("trA2").select("td a").get(mPageLinkCounter).attr("abs:href"); 
         String iconSrcA2 = eMain.getElementsByClass("trA2").select("td a img").get(i).attr("abs:src"); 

         GameObject gameObjectA2 = new GameObject(); 
         gameObjectA2.setGlTitle(titleA2); 
         gameObjectA2.setGlAchAmount(achAmountA2 + " achievements"); 
         gameObjectA2.setGlGSAmount(gsAmountA2 + " gamer score"); 
         gameObjectA2.setGlAchPageUrl(aPageLinkA2); 
         gameObjectA2.setGlIcon(iconSrcA2); 
         mGameObjectList.add(gameObjectA2); 
        } 

        // counters 
        mPageLinkCounter += 3; 
        mAchAmountCounter += 2; 
        mGsAmountCounter += 2; 

        // update progress bar 
        publishProgress((100/(Integer) Math.round(eA1.size()))); 
       } 
      } 
+0

귀하의 질문에 전체 스택 추적을 포함하십시오. – fge

답변

1

"trA2"의 크기는 항상 "trA1"의 크기보다 작으므로 수정 사항으로 사용할 수 있습니다.

오른쪽 루프의 이전

, 선언

final int size2 = eMain.getElementsByClass("trA2").size(); 

를 루프, 변경 :

if (i < size2 && eMain.getElementsByClass("trA2").select("strong").get(i).text() != null) 

논외하지만 코드 명확성을 위해 :

if (eMain.getElementsByClass("trA2").select("strong").get(i).text() != null) 

에 , 두 변수를 선언 할 수 있습니다. element1element2 ~ eMain.getElementsByClass("trA1")eMain.getElementsByClass("trAeMain.getElementsByClass("trA2")은 코드를 명확하게하기 위해 for 루프 바로 앞에 있습니다.

+0

덕분에 그것이 매력처럼 작동했습니다. – iamrelos

+0

문제 없음;) 그러나 더 많은 지역 변수를 사용하려고 시도하면 코드가 어지럽게 정리됩니다. – fge

관련 문제