Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
604 views
in Technique[技术] by (71.8m points)

android - index out of bounds exception. array

I am trying to add the items from my array into the listview but i always get an exception.

below is the code of how i add the items:

items.add(new SectionItem("President"));
                for(int a = 0; a <= presList.size(); a++)
                {
                    String name = presList.get(a);
                    String companyName = presList.get(a + 1);
                    Log.d("company name:", companyName);
                    items.add(new EntryItem(name,companyName));
                }

Logcat Output:

11-24 04:36:44.981: I/System.out(719): [潘国驹博士, 世界科技出版公司(董事主席)]
11-24 04:36:44.990: D/company name:(719): 世界科技出版公司(董事主席)
11-24 04:36:44.990: D/AndroidRuntime(719): Shutting down VM
11-24 04:36:44.990: W/dalvikvm(719): threadid=1: thread exiting with uncaught exception (group=0x409c01f8)
11-24 04:36:45.010: E/AndroidRuntime(719): FATAL EXCEPTION: main
11-24 04:36:45.010: E/AndroidRuntime(719): java.lang.IndexOutOfBoundsException: Invalid index 2, size is 2
11-24 04:36:45.010: E/AndroidRuntime(719):  at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:251)
11-24 04:36:45.010: E/AndroidRuntime(719):  at java.util.ArrayList.get(ArrayList.java:304)
11-24 04:36:45.010: E/AndroidRuntime(719):  at com.scba.organization.CouncilList$LoadAllMembers$1.run(CouncilList.java:192)
11-24 04:36:45.010: E/AndroidRuntime(719):  at android.app.Activity.runOnUiThread(Activity.java:4170)
11-24 04:36:45.010: E/AndroidRuntime(719):  at com.scba.organization.CouncilList$LoadAllMembers.onPostExecute(CouncilList.java:184)
11-24 04:36:45.010: E/AndroidRuntime(719):  at com.scba.organization.CouncilList$LoadAllMembers.onPostExecute(CouncilList.java:1)
11-24 04:36:45.010: E/AndroidRuntime(719):  at android.os.AsyncTask.finish(AsyncTask.java:602)
11-24 04:36:45.010: E/AndroidRuntime(719):  at android.os.AsyncTask.access$600(AsyncTask.java:156)
11-24 04:36:45.010: E/AndroidRuntime(719):  at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:615)
11-24 04:36:45.010: E/AndroidRuntime(719):  at android.os.Handler.dispatchMessage(Handler.java:99)
11-24 04:36:45.010: E/AndroidRuntime(719):  at android.os.Looper.loop(Looper.java:137)
11-24 04:36:45.010: E/AndroidRuntime(719):  at android.app.ActivityThread.main(ActivityThread.java:4424)
11-24 04:36:45.010: E/AndroidRuntime(719):  at java.lang.reflect.Method.invokeNative(Native Method)
11-24 04:36:45.010: E/AndroidRuntime(719):  at java.lang.reflect.Method.invoke(Method.java:511)
11-24 04:36:45.010: E/AndroidRuntime(719):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
11-24 04:36:45.010: E/AndroidRuntime(719):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
11-24 04:36:45.010: E/AndroidRuntime(719):  at dalvik.system.NativeStart.main(Native Method)
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

use

for(int a = 0; a <= presList.size()-1; a++)

OR

for(int a = 0; a < presList.size(); a++)

instead of

for(int a = 0; a <= presList.size(); a++)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...