0

2我需要一些有关此代码的帮助。我收到一个 IndexOutOfBoundsException。我知道为什么,但我一直在努力解决它。问题:eMain.getElementsByClass("trA1").size() = 8 和 eMain.getElementsByClass("trA2").size() = 7(尺寸很多,但 trA1 总是大于 trA2)。我需要将每个元素添加到 ArrayList 作为 add(trA1), add(trA2), add(trA1), add(trA2)...... (我正在使用 for 循环)但是当我得到到元素 8,我得到了异常,因为没有 trA2 元素 8。我尝试了一个 If 语句来检查元素是 null 还是“”,但我仍然得到了异常。有任何想法吗。

                // 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())));
                }
            }
4

1 回答 1

1

由于“trA2”的大小始终小于“trA1”的大小,因此您可以这样做作为修复。

在 for 循环之前,声明:

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

在 for 循环中,更改:

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

至:

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

题外话,但为了代码清晰起见,您可能还想在 for 循环之前分别声明两个变量element1element2toeMain.getElementsByClass("trA1")eMain.getElementsByClass("trAeMain.getElementsByClass("trA2"),以使您的代码更清晰。

于 2013-06-08T00:28:05.910 回答