gpt4 book ai didi

android - 索引越界异常。大批

转载 作者:太空狗 更新时间:2023-10-29 15:36:45 26 4
gpt4 key购买 nike

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

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

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 输出:

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)

最佳答案

使用

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

关于android - 索引越界异常。大批,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13538335/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com