Android 我能';无法获取ArrayList中的元素
在这里,我的代码用于在名为Test的数组列表中选择值,我需要根据按钮按下事件获取数组列表中的值,但它显示了下面给出的错误Android 我能';无法获取ArrayList中的元素,android,arraylist,data-retrieval,Android,Arraylist,Data Retrieval,在这里,我的代码用于在名为Test的数组列表中选择值,我需要根据按钮按下事件获取数组列表中的值,但它显示了下面给出的错误 bn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { d4 = ex.getText().toString(); int i=test.size();
bn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
d4 = ex.getText().toString();
int i=test.size();
for(int j=0;j<=i;j++) {
String s= test.get(j).toString();
Toast.makeText(getApplicationContext(),s + " ", Toast.LENGTH_SHORT).show();
}
}
});
04-04 16:38:29.551 28723-28723/com.example.web03.bulkmessenger E/MotionRecognitionManager: mSContextService = null
04-04 16:38:29.551 28723-28723/com.example.web03.bulkmessenger E/MotionRecognitionManager: motionService = com.samsung.android.motion.IMotionRecognitionService$Stub$Proxy@3e5f9413
04-04 16:38:32.051 28723-28723/com.example.web03.bulkmessenger E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.web03.bulkmessenger, PID: 28723
java.lang.IndexOutOfBoundsException: Invalid index 2, size is 2
at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
at java.util.ArrayList.get(ArrayList.java:308)
at com.example.web03.bulkmessenger.MessengerActivity$3.onClick(MessengerActivity.java:127)
at android.view.View.performClick(View.java:5246)
at android.widget.TextView.performClick(TextView.java:10573)
at android.view.View$PerformClick.run(View.java:21256)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:6912)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)
提前感谢您的帮助,因为您调用了数组大小+1..所以请删除j因为您已将for循环中的变量j设置为“j,只需将for循环更改为
for(int j=0;j<test.size();j++)
for(int j=0;j
java.lang.IndexOutOfBoundsException:索引2无效,大小为2
当程序试图使用超出有效索引范围的值访问可索引集合中的值时引发
问题
for(int j=0;j<=i;j++) { // == Usually any negative integer less than or equal to -1 and positive integer greater than or equal to the size of the object is an index which would be out of bounds.
for(int j=0;j在for循环的最后一次迭代中,数组超出了范围,因为i
的值是test.size();
如果您只是想使用该值迭代地显示一个祝酒词,只需使用for each循环。您在这里得到值了吗test.size();
@IntelliJAmiya:yes>关于的内容是什么(int j=0;j@IntelliJAmiya:谢谢,我工作了
for(int j=0;j<test.size();j++)
for(int j=0;j<=i;j++) { // == Usually any negative integer less than or equal to -1 and positive integer greater than or equal to the size of the object is an index which would be out of bounds.
for(int j=0;j<i;j++) {