Android Index超出Bounds Exception

时间:2014-03-19 14:35:17

标签: java android arrays indexoutofboundsexception

我的索引如何超出范围。我知道我的内容数组中有元素。每次添加索引元素时,我都会遍历此内容3次。为什么我仍然会收到此错误。请帮忙。提前谢谢大家

03-19 09:53:06.041: E/AndroidRuntime(14937): FATAL EXCEPTION: main
03-19 09:53:06.041: E/AndroidRuntime(14937): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.gordondev.tlchackprep/com.gordondev.tlchackprep._App}: java.lang.ArrayIndexOutOfBoundsException: length=3; index=3
03-19 09:53:06.041: E/AndroidRuntime(14937):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2266)
03-19 09:53:06.041: E/AndroidRuntime(14937):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2316)
03-19 09:53:06.041: E/AndroidRuntime(14937):    at android.app.ActivityThread.access$600(ActivityThread.java:150)
03-19 09:53:06.041: E/AndroidRuntime(14937):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1298)
03-19 09:53:06.041: E/AndroidRuntime(14937):    at android.os.Handler.dispatchMessage(Handler.java:99)
03-19 09:53:06.041: E/AndroidRuntime(14937):    at android.os.Looper.loop(Looper.java:213)
03-19 09:53:06.041: E/AndroidRuntime(14937):    at android.app.ActivityThread.main(ActivityThread.java:5225)
03-19 09:53:06.041: E/AndroidRuntime(14937):    at java.lang.reflect.Method.invokeNative(Native Method)
03-19 09:53:06.041: E/AndroidRuntime(14937):    at java.lang.reflect.Method.invoke(Method.java:525)
03-19 09:53:06.041: E/AndroidRuntime(14937):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
03-19 09:53:06.041: E/AndroidRuntime(14937):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:602)
03-19 09:53:06.041: E/AndroidRuntime(14937):    at dalvik.system.NativeStart.main(Native Method)
03-19 09:53:06.041: E/AndroidRuntime(14937): Caused by: java.lang.ArrayIndexOutOfBoundsException: length=3; index=3
03-19 09:53:06.041: E/AndroidRuntime(14937):    at com.gordondev.tlchackprep.SimpleAdapter.<init>(SimpleAdapter.java:66)
03-19 09:53:06.041: E/AndroidRuntime(14937):    at com.gordondev.tlchackprep._App$PinnedListView.initializeAdapter(_App.java:280)
03-19 09:53:06.041: E/AndroidRuntime(14937):    at com.gordondev.tlchackprep._App$PinnedListView.initializeHeaderAndFooter(_App.java:262)
03-19 09:53:06.041: E/AndroidRuntime(14937):    at com.gordondev.tlchackprep._App$PinnedListView.onActivityCreated(_App.java:168)
03-19 09:53:06.041: E/AndroidRuntime(14937):    at android.app.Fragment.performActivityCreated(Fragment.java:1707)
03-19 09:53:06.041: E/AndroidRuntime(14937):    at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:907)
03-19 09:53:06.041: E/AndroidRuntime(14937):    at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1061)
03-19 09:53:06.041: E/AndroidRuntime(14937):    at android.app.BackStackRecord.run(BackStackRecord.java:682)
03-19 09:53:06.041: E/AndroidRuntime(14937):    at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1443)
03-19 09:53:06.041: E/AndroidRuntime(14937):    at android.app.Activity.performStart(Activity.java:5142)
03-19 09:53:06.041: E/AndroidRuntime(14937):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2239)

这是我的代码。 错误行:项目项目=新项目(Item.ITEM,内容[索引]);

String[] title = { "Scope of this Chapter", "Penalties" };

    String[] content = {
            "Other than words that are capitalized in the normal course "
                    + "(such as “Mayor of the City of New York” or the first word in a sentence) "
                    + "any word (or group of words) in these Rules that has its first letter capitalized will be a “defined term.”",
            "\nMost defined terms appear in this Chapter. For ease of reference, certain defined terms may also appear in the"
                    + " “Definitions Applicable to this Chapter” section of Chapters in which the terms are most relevant. "
                    + "Certain general terms (Driver, License, Owner, for example) will have a more specific meaning in individual "
                    + "Chapters (so, Driver in the Chapters governing Taxicabs and their Drivers will mean a Taxicab Driver). In some "
                    + "cases, a defined term in a Chapter can have a meaning different from that in this Chapter (for example, a Broker "
                    + "in Chapter 65 is different). Those different definitions will appear in the relevant Chapters.",
            "This Chapter is informational in nature and does not contain penalties" };

    final int sectionsNumber = title.length;
    final int arrayIter[] = { 2, 1 };
    int index = 0;
    int check = 0;
    int j;
    int k;

    prepareSections(sectionsNumber);
    int sectionPosition = 0, listPosition = 0;

    for (int i = 0; i < sectionsNumber; i++) {
        Item section = new Item(Item.SECTION, title[i]);
        section.sectionPosition = sectionPosition;
        section.listPosition = listPosition++;
        onSectionAdded(section, sectionPosition);
        add(section);

        for (j = 0; j < arrayIter.length; j++) {
            check = arrayIter[j];

            for (k = 1; k <= check; k++) {
                Item item = new Item(Item.ITEM, content[index]);
                item.sectionPosition = sectionPosition;
                item.listPosition = listPosition++;
                add(item);
                index++;
            }
        }

        sectionPosition++;

    }

3 个答案:

答案 0 :(得分:0)

Justin Jasmann是对的。您正在使用变量“index”来索引数组“content”。 “index”是递增的arrayIter.length *检查次数(通过内部循环时间的次数,通过外部循环的次数)。该数字大于数组的长度。

答案 1 :(得分:0)

你可以通过这种情况处理这种情况......

if(index < content.length())
    Item item = new Item(Item.ITEM, content[index]);
    item.sectionPosition = sectionPosition;
    item.listPosition = listPosition++;
    add(item);
    index++;
}

答案 2 :(得分:0)

index = 0循环之前初始化for (k = 1; k <= check; k++);如果不是,下次进入该循环索引会给你带来问题