2

我正在尝试将数组中的项目添加到列表视图中,但我总是遇到异常。

下面是我如何添加项目的代码:

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

日志输出:

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)
4

2 回答 2

9

采用

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

或者

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

代替

for(int a = 0; a <= presList.size(); a++)
于 2012-11-24T04:43:31.613 回答
4

它应该是

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

假设我们有ArrayList5 个项目

所以访问的元素ArrayList从索引 0 到 4 开始

于 2012-11-24T04:43:14.393 回答