数组索引超出了listview的范围

时间:2015-10-15 13:59:22

标签: android

public class KanalAdapter extends BaseAdapter {

Context context;
ArrayList<OfficialKanal> officialKanals;
ArrayList<NormalKanal> normalKanals;
ArrayList<Object> kanallar = new ArrayList();
int OFFICIAL_KANAL = 1;
int NORMAL_KANAL= 2;
LayoutInflater lala;
public KanalAdapter(Context context , ArrayList<OfficialKanal> officiallar , ArrayList<NormalKanal> normaller){
    this.context = context;
    officialKanals = officiallar;
    normalKanals = normaller;
    for(int i = 0 ; i < officiallar.size() ; i++){
        kanallar.add(officiallar.get(i));
    }
    for(int i = 0 ; i < normaller.size() ; i++){
        kanallar.add(normaller.get(i));
    }
    Log.i("tago" , "tagtag");
    lala = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

public int getCount() {
    return kanallar.size();
}

public Object getItem(int i) {
    return kanallar.get(i);
}

public long getItemId(int i) {
    return 0;
}

public int getItemViewType(int position) {
    Object item = getItem(position);
    if(item instanceof NormalKanal){
        return NORMAL_KANAL;
    }else if (item instanceof OfficialKanal){
        return OFFICIAL_KANAL;
    }
    return -1;
}

@Override
public int getViewTypeCount() {
    return 2;
}

public View getView(int position, View convertView, ViewGroup viewGroup) {
    KanalHolder holder = null;
    Object currentKanal = getItem(position);
    if(convertView==null) {
        holder = new KanalHolder();
        if (currentKanal instanceof NormalKanal) {
            convertView = lala.inflate(R.layout.normalkanal, null);
            holder.image1 = (ImageView) convertView.findViewById(R.id.imageView5);
            holder.tv1 = (TextView) convertView.findViewById(R.id.textView4);
            Log.i("tago", "tagtagtag");
            holder.tv2 = (TextView) convertView.findViewById(R.id.textView8);
            holder.buton1 = (Button) convertView.findViewById(R.id.button8);
        }
        if (currentKanal instanceof OfficialKanal) {
            convertView = lala.inflate(R.layout.officialkanal, null);
            holder.image1 = (ImageView) convertView.findViewById(R.id.imageView5);
            holder.tv1 = (TextView) convertView.findViewById(R.id.textView4);
            Log.i("tago", "tagtagatagtagtagatg");
            holder.tv2 = (TextView) convertView.findViewById(R.id.textView8);
        }

        convertView.setTag(holder);
    }else{
        holder = (KanalHolder)convertView.getTag();
    }
    //holder.tv1.setText(normalKanals.get(position).getKanaladi());
        holder.image1.setImageResource(R.mipmap.aliprof);
    return convertView;
}


static class KanalHolder{
    public ImageView image1;
    public TextView tv1 , tv2;
    public Button buton1;
}
}

它会出现如下错误:

 10-15 16:52:44.737 21087-21087/com.example.bahadir.myapplicationn      E/AndroidRuntime: FATAL EXCEPTION: main
 10-15 16:52:44.737 21087-21087/com.example.bahadir.myapplicationn      E/AndroidRuntime: Process: com.example.bahadir.myapplicationn, PID: 21087
 10-15 16:52:44.737 21087-21087/com.example.bahadir.myapplicationn E/AndroidRuntime: java.lang.ArrayIndexOutOfBoundsException: length=2; index=2

我知道它说我试图在索引2中获取对象,但没有索引2(0和1)。但我的数组大小是官方用户的10.5对象。 5为普通人。哪里弄错了?

FATAL EXCEPTION: main
10-15 17:10:00.377 29887-29887/com.example.bahadir.myapplicationn E/AndroidRuntime: Process: com.example.bahadir.myapplicationn, PID: 29887
10-15 17:10:00.377 29887-29887/com.example.bahadir.myapplicationn E/AndroidRuntime: java.lang.ArrayIndexOutOfBoundsException: length=2; index=2
10-15 17:10:00.377 29887-29887/com.example.bahadir.myapplicationn E/AndroidRuntime:     at android.widget.AbsListView$RecycleBin.addScrapView(AbsListView.java:8654)
10-15 17:10:00.377 29887-29887/com.example.bahadir.myapplicationn E/AndroidRuntime:     at android.widget.AbsListView.trackMotionScroll(AbsListView.java:6978)
10-15 17:10:00.377 29887-29887/com.example.bahadir.myapplicationn E/AndroidRuntime:     at android.widget.AbsListView.scrollIfNeeded(AbsListView.java:4179)
10-15 17:10:00.377 29887-29887/com.example.bahadir.myapplicationn E/AndroidRuntime:     at android.widget.AbsListView.onTouchMove(AbsListView.java:5541)
10-15 17:10:00.377 29887-29887/com.example.bahadir.myapplicationn E/AndroidRuntime:     at android.widget.AbsListView.onTouchEvent(AbsListView.java:5376)
10-15 17:10:00.377 29887-29887/com.example.bahadir.myapplicationn E/AndroidRuntime:     at android.view.View.dispatchTouchEvent(View.java:8920)
10-15 17:10:00.377 29887-29887/com.example.bahadir.myapplicationn E/AndroidRuntime:     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2673)
10-15 17:10:00.377 29887-29887/com.example.bahadir.myapplicationn E/AndroidRuntime:     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2385)
10-15 17:10:00.377 29887-29887/com.example.bahadir.myapplicationn E/AndroidRuntime:     at android.widget.AbsListView.dispatchTouchEvent(AbsListView.java:5318)
10-15 17:10:00.377 29887-29887/com.example.bahadir.myapplicationn E/AndroidRuntime:     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2684)
10-15 17:10:00.377 29887-29887/com.example.bahadir.myapplicationn E/AndroidRuntime:     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2400)
10-15 17:10:00.377 29887-29887/com.example.bahadir.myapplicationn E/AndroidRuntime:     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2684)
10-15 17:10:00.377 29887-29887/com.example.bahadir.myapplicationn E/AndroidRuntime:     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2400)
10-15 17:10:00.377 29887-29887/com.example.bahadir.myapplicationn E/AndroidRuntime:     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2684)
10-15 17:10:00.377 29887-29887/com.example.bahadir.myapplicationn E/AndroidRuntime:     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2400)
10-15 17:10:00.377 29887-29887/com.example.bahadir.myapplicationn E/AndroidRuntime:     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2684)
10-15 17:10:00.377 29887-29887/com.example.bahadir.myapplicationn E/AndroidRuntime:     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2400)
10-15 17:10:00.377 29887-29887/com.example.bahadir.myapplicationn E/AndroidRuntime:     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2684)
10-15 17:10:00.377 29887-29887/com.example.bahadir.myapplicationn E/AndroidRuntime:     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2400)
10-15 17:10:00.377 29887-29887/com.example.bahadir.myapplicationn E/AndroidRuntime:     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2684)
10-15 17:10:00.377 29887-29887/com.example.bahadir.myapplicationn E/AndroidRuntime:     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2400)
10-15 17:10:00.377 29887-29887/com.example.bahadir.myapplicationn E/AndroidRuntime:     at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2552)
10-15 17:10:00.377 29887-29887/com.example.bahadir.myapplicationn E/AndroidRuntime:     at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1767)
10-15 17:10:00.377 29887-29887/com.example.bahadir.myapplicationn E/AndroidRuntime:     at android.app.Activity.dispatchTouchEvent(Activity.java:2840)
10-15 17:10:00.377 29887-29887/com.example.bahadir.myapplicationn E/AndroidRuntime:     at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2513)
10-15 17:10:00.377 29887-29887/com.example.bahadir.myapplicationn E/AndroidRuntime:     at android.view.View.dispatchPointerEvent(View.java:9125)
10-15 17:10:00.377 29887-29887/com.example.bahadir.myapplicationn E/AndroidRuntime:     at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:4632)
10-15 17:10:00.377 29887-29887/com.example.bahadir.myapplicationn E/AndroidRuntime:     at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:4470)
10-15 17:10:00.377 29887-29887/com.example.bahadir.myapplicationn E/AndroidRuntime:     at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4020)
10-15 17:10:00.377 29887-29887/com.example.bahadir.myapplicationn E/AndroidRuntime:     at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4073)
10-15 17:10:00.377 29887-29887/com.example.bahadir.myapplicationn E/AndroidRuntime:     at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4039)
10-15 17:10:00.377 29887-29887/com.example.bahadir.myapplicationn E/AndroidRuntime:     at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:4153)
10-15 17:10:00.377 29887-29887/com.example.bahadir.myapplicationn E/AndroidRuntime:     at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:4047)
10-15 17:10:00.377 29887-29887/com.example.bahadir.myapplicationn E/AndroidRuntime:     at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:4210)
10-15 17:10:00.377 29887-29887/com.example.bahadir.myapplicationn E/AndroidRuntime:     at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4020)
10-15 17:10:00.377 29887-29887/com.example.bahadir.myapplicationn E/AndroidRuntime:     at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4073)
10-15 17:10:00.377 29887-29887/com.example.bahadir.myapplicationn E/AndroidRuntime:     at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4039)
10-15 17:10:00.377 29887-29887/com.example.bahadir.myapplicationn E/AndroidRuntime:     at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:4047)
10-15 17:10:00.377 29887-29887/com.example.bahadir.myapplicationn E/AndroidRuntime:     at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4020)
10-15 17:10:00.377 29887-29887/com.example.bahadir.myapplicationn E/AndroidRuntime:     at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:6480)
10-15 17:10:00.377 29887-29887/com.example.bahadir.myapplicationn E/AndroidRuntime:     at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:6379)
10-15 17:10:00.377 29887-29887/com.example.bahadir.myapplicationn E/AndroidRuntime:     at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:6350)
10-15 17:10:00.377 29887-29887/com.example.bahadir.myapplicationn E/AndroidRuntime:     at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:6570)
10-15 17:10:00.377 29887-29887/com.example.bahadir.myapplicationn E/AndroidRuntime:     at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:185)
10-15 17:10:00.377 29887-29887/com.example.bahadir.myapplicationn E/AndroidRuntime:     at android.view.InputEventReceiver.nativeConsumeBatchedInputEvents(Native Method)
10-15 17:10:00.377 29887-29887/com.example.bahadir.myapplicationn E/AndroidRuntime:     at android.view.InputEventReceiver.consumeBatchedInputEvents(InputEventReceiver.java:176)
10-15 17:10:00.377 29887-29887/com.example.bahadir.myapplicationn E/AndroidRuntime:     at android.view.ViewRootImpl.doConsumeBatchedInput(ViewRootImpl.java:6541)
10-15 17:10:00.377 29887-29887/com.example.bahadir.myapplicationn E/AndroidRuntime:     at android.view.ViewRootImpl$ConsumeBatchedInputRunnable.run(ViewRootImpl.java:6593)
10-15 17:10:00.377 29887-29887/com.example.bahadir.myapplicationn E/AndroidRuntime:     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:777)
10-15 17:10:00.377 29887-29887/com.example.bahadir.myapplicationn E/AndroidRuntime:     at android.view.Choreographer.doCallbacks(Choreographer.java:590)
10-15 17:10:00.377 29887-29887/com.example.bahadir.myapplicationn E/AndroidRuntime:     at android.view.Choreographer.doFrame(Choreographer.java:558)

1 个答案:

答案 0 :(得分:4)

问题出在您的项目视图类型中。

Adapter.getItemViewType(int)

  

注意:整数必须在0到getViewTypeCount() - 1

的范围内

OFFICIAL_KANAL更改为0,将NORMAL_KANAL更改为1.