如何解决 ArrayIndexOfBoundsException

how solve ArrayIndexOfBoundsException

我正在尝试开发一个包含游戏中的点数和日期的 ListView。但我想在每个点旁边放一张图片。这些图像我在一个数组中,但是当我执行应用程序时它变慢了,应用程序停止并完成并且它显示 ArrayIndexOfBoundsException length = 54 index = 54。 这就是我的代码:

public class TodoAdapter extends BaseAdapter {

private Vector<String> todoList;
Context contexto;
TypedArray trofeos;

public TodoAdapter(Context context, Vector<String> todoList) {

    this.todoList = todoList;
    contexto = context;
    trofeos=contexto.getResources().obtainTypedArray(R.array.imagenes);
}

@Override
public int getCount() {
    return todoList.size();
}

@Override
public Object getItem(int position) {

    return todoList.elementAt(position);
}

@Override
public long getItemId(int position) {
    return position;
}

@SuppressLint("InflateParams")
@Override
public View getView(int position, View convertView, ViewGroup parent) {
    HolderA holder;
    if (convertView == null) {
        LayoutInflater inflater = LayoutInflater.from(contexto);
        convertView = inflater.inflate(R.layout.elemento_lista, null);
        holder = new HolderA();
        holder.imagen=(ImageView)convertView.findViewById(R.id.iv);
        holder.puntos = (TextView) convertView.findViewById(R.id.tv1);
        holder.fecha = (TextView) convertView.findViewById(R.id.tv2);
        convertView.setTag(holder);
    } else {
        holder = (HolderA) convertView.getTag();
    }
    holder.imagen.setImageDrawable(trofeos.getDrawable(position));
    holder.puntos.setText(getItem(position).toString().substring(0,
            getItem(position).toString().length() - 19));
    holder.fecha.setText(getItem(position).toString().substring(
            getItem(position).toString().length() - 19,
            getItem(position).toString().length()));
    return convertView;
}

static class HolderA {
    ImageView imagen;
    TextView puntos;
    TextView fecha;
}

eclipse 显示 holder.imagen.setImageDrawable(trofeos.getDrawable(position)) 中的错误; 任何的想法??拜托

05-16 22:42:32.710: E/InputEventReceiver(2784): Exception dispatching input event.
05-16 22:42:32.790: E/AndroidRuntime(2784): FATAL EXCEPTION: main
05-16 22:42:32.790: E/AndroidRuntime(2784): java.lang.ArrayIndexOutOfBoundsException: length=54; index=54
holder.fecha.setText(getItem(position).toString().substring(
            getItem(position).toString().length() - 19,
            getItem(position).toString().length()));

这是错误的地方,正如文档所说:

substring(int beginIndex, int endIndex) Returns a new string that is a substring of this string.

可以看到,需要用getItem(position).toString().length() - 1代替getItem(position).toString().length(),因为index是0开头,count是1开头

试一试

        holder.imagen.setImageDrawable(trofeos.getDrawable(position-1));
holder.puntos.setText(getItem(position-1).toString().substring(0,
        getItem(position-1).toString().length() - 19));
    holder.fecha.setText(getItem(position-1).toString().substring(
        getItem(position-1).toString().length() - 19,
        getItem(position-1).toString().length()));

问题是您在 xml 的数组中只有 9 个项目。 这导致 todoListtrofeos 之间的大小不匹配,这是导致 ArrayIndexOutOfBoundsException 的原因,因为您的适配器数据源的大小为 10:

maxRegis=bd.listaPuntos(10);
TodoAdapter adaptador= new TodoAdapter(this,maxRegis);

要么使适配器的数据源大小为 9,要么向您的可绘制对象列表添加另一项:

<array name="imagenes">
    <item name="elemento1">@drawable/oro</item>
    <item name="elemento2">@drawable/plata</item>
    <item name="elemento3">@drawable/bronce</item>
    <item name="elemento3">@drawable/bronce</item>
    <item name="elemento3">@drawable/bronce</item>
    <item name="elemento3">@drawable/bronce</item>
    <item name="elemento3">@drawable/bronce</item>
    <item name="elemento3">@drawable/bronce</item>
    <item name="elemento3">@drawable/bronce</item>
</array>