2014-03-19 48 views
0

一些如何我的索引超出界限。我知道我的内容数组中有元素。每次添加索引元素时,我都会遍历这个内容3次。为什么我仍然得到这个错误。请帮忙。谢谢大家提前Android索引溢出异常

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 = new Item(Item.ITEM,content [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++; 

    } 
+1

所以'内容'是一个恰好一个字符串的数组?那是你的意思吗? –

+1

你正在增加'索引'很多次,并且从不重置它......你确定你打算这么做吗? –

+0

内容数组中有三个字符串。 –

回答

0

贾斯汀贾斯曼是正确的。您正在使用变量“index”来索引数组“content”。 “index”递增arrayIter.length *检查次数(通过内部循环的次数通过外部循环的次数)。这个数字比数组的长度要大得多。

0

您可以通过此条件下处理的情况......

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

我需要循环在两个不同的设置中绕过三次。第一次两次。然后是第二次。每次保持索引的位置。 –

0

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

+0

这会得到我的错误,但我需要最内层的循环来保留第一次初始运行后的索引元素。您的解决方案从第一个元素重新开始内容数组。我想要最内层的循环运行3次。先两次然后一次。每次跟踪内容数组上的索引。 –

+0

如果你在'for(j = 0; j Narkha

+0

当我像你所说的那样做时,我得到的结果并没有达到我想要做的。我有一个3字符串的数组。我希望for循环在第一次获取前两个索引字符串并将它们添加到特定位置时进行两次迭代。然后再添加最后一个字符串到不同的位置。当我使用该代码时,所得到的三个字符串都被添加到两个位置。 –