exOutOfBoundsException异常在ArrayAd

exOutOfBoundsException异常在ArrayAd

本文介绍了IndexOutOfBoundsException异常在ArrayAdapter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的code涉及的ListView ArrayAdapter 将抛出一个IndexOutOfBoundsException异常在某些设备上。现在的问题是:我不知道这个异常是如何发生的,我只在Android的开发者控制台获得踪迹

的还原$ C $℃实施例显示如下。哪有的的getItem 操作 ArrayAdapter 失败的位置元素?在 ArrayAdapter 是从来没有改变过,没有在活动没有其他方法

我知道什么是 IndexOutOfBoundsException异常是的,我知道我可以先检查长度prevent它。但我很好奇:这怎么能例外发生在这里?有人如何在不的数据结构存在的情况下点击?

减少code:

 公共类EventListActivity延伸活动{
    公共无效调用onStart(){
        最终的ListView列表视图=新的ListView(这一点);
        最终事件[] =事件[从某处检索阵列]
        最后ArrayAdapter<事件>一个=新ArrayAdapter<事件>(这一点,R.layout.eventlistitem,事件);
        listview.setAdapter(一);
        listview.setOnItemClickListener(新OnItemClickListener(){
            公共无效onItemClick(适配器视图<>母公司,观景,INT位置,长的id){
                事件事件= a.getItem(位置);
                                ^^^^^^^抛出异常
            }
        });

例外:

  java.lang.IndexOutOfBoundsException
在java.util.Arrays中的$ ArrayList.get(Arrays.java:75)
在android.widget.ArrayAdapter.getItem(ArrayAdapter.java:298)
在xxx.EventListActivity $ 3.onItemClick(EventListActivity.java:130)


解决方案

好吧,我调试它,万一有人有相同的错误,这里是解决方案:

如果您使用 ListView.addHeader 位置的参数中的 onItemClick 由1转向似乎很奇怪,因为该位置,然后无关与数组适配器的位置。 This SO问题包含了相同的结论,你有你添加为标题要注意了。

以上我的问题其实不是非常有帮助,因为整个和addHeader 部分缺失,这是不可能由别人来回答这个问题,不好意思。然而,这个答案可能是有帮助他人。

I got a trivial code involving a ListView and a ArrayAdapter which throws a IndexOutOfBoundsException on some devices. The problem is: I do not know how this Exception occurs, I only get the stacktraces from the Developer Console of Android.

The reduced code example is shown below. How can the getItem operation of the ArrayAdapter fail for the position element? The ArrayAdapter is never changed, there is no other method in the Activity.

I know what a IndexOutOfBoundsException is, I know that I can prevent it by checking the length first. But I'm curious: How can this Exception happen here? How can someone click on a event which does not exist in the datastructure?

Reduced code:

public class EventListActivity extends Activity {
    public void onStart() {
        final ListView listview = new ListView(this);
        final Event[] events = [Retrieve a Array from somewhere]
        final ArrayAdapter<Event> a = new ArrayAdapter<Event>(this, R.layout.eventlistitem, events);
        listview.setAdapter(a);
        listview.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Event event = a.getItem(position);
                                ^^^^^^^ throws Exception
            }
        });

Exception:

java.lang.IndexOutOfBoundsException
at java.util.Arrays$ArrayList.get(Arrays.java:75)
at android.widget.ArrayAdapter.getItem(ArrayAdapter.java:298)
at xxx.EventListActivity$3.onItemClick(EventListActivity.java:130)
解决方案

Okay, I debugged it, in case someone has the same error, here is the solution:

If you use ListView.addHeader, the position argument of the onItemClick is shifted by 1. Seems quite weird because the position then has nothing to do with the position in the array-adapter. This SO Question contains the same conclusion, you have to pay attention what you add as headers.

My question above was in fact not very helpfull because the whole addHeader part was missing, it was impossible to answer the question by others, sorry. Nevertheless, this answer might be helpfull for others.

这篇关于IndexOutOfBoundsException异常在ArrayAdapter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 18:43