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

最佳答案

由于“ 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循环之前分别向element1element2声明两个变量eMain.getElementsByClass("trA1")eMain.getElementsByClass("trAeMain.getElementsByClass("trA2"),以使代码更清晰。

10-06 07:02