我的列表数组有点麻烦它最初是4个项目长的教程,然后我试着玩它并通过输入更多的标题和图像扩大它的大小,但我得到以下
logcat中的错误:
java.lang.ArrayIndexOutOfBoundsException = length=4; index=4
以下是代码:
package org.iimed.www;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.Toast;
import org.iimed.www.R;
import org.iimed.www.CustomListViewAdapter;
import org.iimed.www.RowItem;
public class ImageTextListViewActivity extends Activity implements
OnItemClickListener {
public static final String[] titles = new String[] { "Penicillins",
"Cephalosporins", "Monobactams", "Carbapenems","Macrolides","Tetracyclines",};
public static final String[] descriptions = new String[] {
"It is an aggregate accessory fruit",
"It is the largest herbaceous flowering plant", "Citrus Fruit",
"Mixed Fruits" };
public static final Integer[] images = { R.drawable.antibiotic_buton,
R.drawable.antibiotic_buton, R.drawable.antibiotic_buton, R.drawable.antibiotic_buton, R.drawable.antibiotic_buton,R.drawable.antibiotic_buton, };
ListView listView;
List<RowItem> rowItems;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.screj);
rowItems = new ArrayList<RowItem>();
for (int i = 0; i <= titles.length; i++) {
RowItem item = new RowItem(images[i], titles[i], descriptions[i]);
rowItems.add(item);
}
listView = (ListView) findViewById(R.id.list);
CustomListViewAdapter adapter = new CustomListViewAdapter(this,
R.layout.list_item, rowItems);
listView.setAdapter(adapter);
listView.setOnItemClickListener(this);
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
Toast toast = Toast.makeText(getApplicationContext(),
"Item " + (position + 1) + ": " + rowItems.get(position),
Toast.LENGTH_SHORT);
toast.setGravity(Gravity.BOTTOM|Gravity.CENTER_HORIZONTAL, 0, 0);
toast.show();
}
}
视为&lt; = titles.length,“标题”的增加不应该增加长度吗?或者布局本身的列表视图是否太小?感谢您的时间。
答案 0 :(得分:1)
您只有4个描述,并且图块和图像数组的长度为6.添加更多描述
public static final String[] descriptions = new String[] {
"It is an aggregate accessory fruit",
"It is the largest herbaceous flowering plant", "Citrus Fruit",
"Mixed Fruits" };
答案 1 :(得分:0)
for (int i = 0; i <= titles.length; i++) {
这就是问题所在。您试图在最后一次迭代中访问索引titles.length
,这是不允许的。数组中的最大可访问索引始终为arrayLength - 1
。这是因为数组中的索引是0-based
。它们以索引0
开头,以索引array.length - 1
结束。
将您的for
更改为此类内容并将其正常工作。
for (int i = 0; i < titles.length; i++) { // without the equals symbol
此外,与description
数组相比,您的titles
数组似乎只有4个元素。一旦完成上述更改,您将获得AIOOBE,因为描述数组只有4个元素。添加几个元素,以便所有数组都具有相同的元素,因为你想为所有数组使用单个循环。
如果某些项目没有说明,请在数组中提供空字符串或空值
String[] descriptions = new String[] {
"It is an aggregate accessory fruit",
"It is the largest herbaceous flowering plant", "Citrus Fruit",
"Mixed Fruits", null, "" }; // Either blank string, or null value
答案 2 :(得分:0)
我认为数组描述中的项目是4,您试图访问上面的描述[4]。因为titles数组包含6个项目,并且您在循环中使用标题的长度。数组索引从0-(size-1)开始,所以只需在循环中交叉检查。
答案 3 :(得分:0)
您的title
数组(长度为6)比description
数组(长度为4)长。因此,当您迭代title
数组的长度并尝试访问description
数组的索引4时,您将获得ArrayIndexOutOfBoundsException
。
这就是出现问题的地方:
for (int i = 0; i <= titles.length; i++) {
RowItem item = new RowItem(images[i], titles[i], descriptions[i]);
rowItems.add(item);
}
要解决此问题,您可能需要在description
数组中添加两个新项目,以匹配titles
和images
数组中的项目数。
答案 4 :(得分:0)
您必须更改for循环,因为标题和说明数组的大小不同
for (int i = 0; i <= titles.length; i++) {
if(i<=descriptions.length)
{
RowItem item = new RowItem(images[i], titles[i], descriptions[i]);
rowItems.add(item);
}else
{
RowItem item = new RowItem(images[i], titles[i], "");
rowItems.add(item);
}
}