1

Hi I am using am developing android application that uses a custom list view, but I keep getting java.lang.ArrayIndexOutOfBoundsException error. Yet I cant find any errors in my code where could I have gone wrong.

public class MainActivity extends Activity {

    ListView lv_event;

    List<HashMap<String, String>> alist;

    String[] event_name = { "The Kampala City Festival",
            "The Awakening (Lantern Meet of Poets Recital)" };
    int[] event_img = { R.drawable.kla_festival, R.drawable.city_rooming };
    String[] event_date = { "October 6th, 2013", "October 4th, 2013" };
    String[] event_venue = { "Watoto Church, Central", "National Theatre" };
    String[] event_time = { "11:30 am", "07:00 pm" };
    String[] event_info = { "The #KampalaCityFestival will on Sunday 6th October",
             "From the creators of Broken Voices of the Revolution "
             };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        setUI();
        setEventList();
    }

    private void setEventList() {
        // TODO Auto-generated method stub
        try {
            alist = new ArrayList<HashMap<String, String>>();
            for (int i = 0; i < 3; i++) {
                HashMap<String, String> hm = new HashMap<String, String>();
                hm.put("event_picture", Integer.toString(event_img[i]));
                hm.put("event_name", event_name[i]);
                hm.put("event_date", event_date[i]);
                hm.put("event_venue", event_venue[i]);
                hm.put("event_time", event_time[i]);
                hm.put("event_info", event_info[i]);
                alist.add(hm);
            }

            // from
            String[] from = { "event_picture", "event_name", "event_date",
                    "event_venue", "event_time", "event_info" };
            // to
            int[] to = { R.id.img_view_event, R.id.tv_event_name, R.id.tv_date,
                    R.id.tv_event_place, R.id.tv_time, R.id.tv_event_info };

            SimpleAdapter adapter = new SimpleAdapter(getApplicationContext(),
                    alist, R.layout.lv_custom, from, to);

            lv_event.setAdapter(adapter);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            Toast.makeText(getApplicationContext(), e.toString(),
                    Toast.LENGTH_LONG).show();
        }

    }

    private void setUI() {
        // TODO Auto-generated method stub
        lv_event = (ListView) findViewById(R.id.lv_event);
    }
4

7 回答 7

0

您正在尝试对数组进行三次迭代,期望每个数组中有三个元素。

但是在这里您只定义了两个值,例如:

String[] event_name = { "The Kampala City Festival",
        "The Awakening (Lantern Meet of Poets Recital)" };

这可能就是你得到的原因ArrayIndexOutOfBoundsException

因此,如果您想在数组中只有 2 个元素,或者向只有 2 个值的数组添加一个元素,请尝试更改i < 3为。i < 2

于 2013-10-01T09:25:55.417 回答
0

在类开头声明的数组只包含两个元素。然而,循环将尝试访问第三个元素。只需调整回路中的防护装置。

for (int i = 0; i < 2; i++) {
    HashMap<String, String> hm = new HashMap<String, String>();
    hm.put("event_picture", Integer.toString(event_img[i]));
    hm.put("event_name", event_name[i]);
    hm.put("event_date", event_date[i]);
    hm.put("event_venue", event_venue[i]);
    hm.put("event_time", event_time[i]);
    hm.put("event_info", event_info[i]);
    alist.add(hm);
}
于 2013-10-01T09:26:11.550 回答
0

尝试这个,

for (int i = 0; i < 3; i++) {
        HashMap<String, String> hm = new HashMap<String, String>();
        hm.put("event_picture", Integer.toString(event_img[i]));
        hm.put("event_name", event_name[i]);
        hm.put("event_date", event_date[i]);
        hm.put("event_venue", event_venue[i]);
        hm.put("event_time", event_time[i]);
        hm.put("event_info", event_info[i]);
        alist.add(hm);
  } 

更改这个而不是您的代码

   for (int i = 0; i < 2; i++) {
        HashMap<String, String> hm = new HashMap<String, String>();
        hm.put("event_picture", Integer.toString(event_img[i]));
        hm.put("event_name", event_name[i]);
        hm.put("event_date", event_date[i]);
        hm.put("event_venue", event_venue[i]);
        hm.put("event_time", event_time[i]);
        hm.put("event_info", event_info[i]);
        alist.add(hm);
  } 
于 2013-10-01T09:26:14.777 回答
0

查看所有阵列,您for需要进行更改

for (int i = 0; i < 2; i++) { // since there are only 2 elements in all your arrays.

数组的索引从 0 开始。因此,数组中有 2 个元素意味着,您在 arr[0] 和 arr[1] 处有元素。arr[2] 会给你ArrayIndexOutOfBoundsException.

于 2013-10-01T09:25:03.270 回答
0

您的数组的长度为 2,而不是 3。

改变你的从

for (int i = 0; i < 3; i++)

for (int i = 0; i < 2; i++)

-

array[0]; // first element
array[1]; // second element
array[2]; // out of bounds
于 2013-10-01T09:25:05.317 回答
0

只是改变

for (int i = 0; i < 3; i++)for (int i = 0; i < 2; i++)

或者

for (int i = 0; i < event_name.length; i++)而已

于 2013-10-01T09:38:02.630 回答
0

使用 foreach 来避免这种异常。

新的foreach:

for (Object anObject : objectArray) {
    anObject.doStuff();
}

老前锋:

for (int i = 0; objectArray.Length()>i; i++) {
    objectArray[i].doStuff();
}

也许您必须对数组的长度使用不同的方法,例如size()...

于 2015-08-09T15:14:34.557 回答