如何解决ArrayIndexOfBoundsException

时间:2015-05-16 21:02:05

标签: android indexoutofboundsexception

我尝试使用游戏中的点数和日期来开发ListView。但我想在每个点旁边放一张照片。这些图像我在一个数组中,但当我执行应用程序它变慢,应用程序停止并完成它显示ArrayIndexOfBoundsException长度= 54索引= 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))中显示错误; 任何的想法?? pleaseeeee

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

3 个答案:

答案 0 :(得分:2)

holder.fecha.setText(getItem(position).toString().substring(
            getItem(position).toString().length() - 19,
            getItem(position).toString().length()));

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

  

substring(int beginIndex,int endIndex)返回一个新的字符串   这个字符串的子串。

如您所见,您需要使用getItem(position).toString().length() - 1而不是getItem(position).toString().length(),因为索引从0开始,但计数从1开始

答案 1 :(得分:1)

放手一搏

vehicleAnimatedMarker.addTo(map).bindPopup("truck " + getVehicleNameByVehicleId(details.id) + " is moving.").openPopup();

答案 2 :(得分:0)

问题是xml中的数组中只有9个项目。 这导致trofeosArrayIndexOutOfBoundsException之间的大小未匹配,这是导致maxRegis=bd.listaPuntos(10); TodoAdapter adaptador= new TodoAdapter(this,maxRegis); 的原因,因为您的适配器数据源的大小为10:

<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>

使适配器的数据源大小为9,或者将另一项添加到您的drawables列表中:

module Logger
  def log_item(person)
    logger.debug "#{person.name} entered Office: #{Time.now}"
  end
end

class Room
  extend Logger
  @log = [] #you could use this to collect statements then log at a later time? otherwise I'm not sure why you would take this approach.
  @occupants = []

  def self.log_entry(person)
    if @occupants << person
      log_item(person)
    end
  end
end