繁体   English   中英

Android索引超出界限异常

[英]Android Index out of Bounds Exception

我的索引如何不断超越极限。 我知道我的内容数组中有元素。 每次添加索引元素时,我都会遍历此内容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.ITEM,内容[index]);

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++;

    }

贾斯汀·贾斯曼是正确的。 您正在使用变量“ index”来索引数组“ content”。 “索引”是增加的arrayIter.length *检查次数(通过内循环的次数乘以通过外循环的次数)。 该数字远大于数组的长度。

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM