0

我的列表数组有点问题,它最初是教程中的 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();
    }
}

看到<=titles.length,“titles”的增加不应该增加长度吗?还是布局本身的列表视图太小?感谢您的时间。

4

5 回答 5

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" };
于 2013-10-23T04:33:44.337 回答
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

此外,与具有 6description个元素的数组相比,您的数组似乎只有 4 个元素titles。完成上述更改后,您将获得 AIOOBE,因为描述数组只有 4 个元素。添加更多元素,以便所有数组都具有相同的元素,以防您想对所有数组使用单个循环。

如果某些项目没有描述,请在数组中提供空白字符串或 null

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
于 2013-10-23T04:25:53.940 回答
0

我认为数组描述中的项目是 4 并且您正在尝试访问上面的描述 [4]。因为标题数组包含 6 个项目,并且您在循环中使用标题的长度。数组索引从 0-(size-1) 开始,所以只需在循环中交叉检查即可。

于 2013-10-23T04:26:57.010 回答
0

您的title数组(长度为 6)比您的description数组(长度为 4)长。因此,当您迭代数组的长度title并尝试访问数组的索引 4 时description,您将得到一个ArrayIndexOutOfBoundsException.

这是出现问题的地方:

for (int i = 0; i <= titles.length; i++) {
    RowItem item = new RowItem(images[i], titles[i], descriptions[i]);
    rowItems.add(item);
}

要修复它,您可能需要在数组中添加两个新项目description以匹配您的titlesimages数组中的项目数。

于 2013-10-23T04:27:07.767 回答
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);
      }
     }
于 2013-10-23T04:43:46.640 回答