ERROR / AndroidRuntime(1884):滚动列表视图时java.lang.ArrayIndexOutOfBoundsException

时间:2011-07-05 23:51:57

标签: android listview android-arrayadapter

您好我在listview中使用自定义Adapter类作为适配器当我在5-6个位置后滚动列表时,我的应用程序强制关闭以查看我得到的LogCat输出

07-06 04:48:36.635: ERROR/AndroidRuntime(1884): FATAL EXCEPTION: main<br>
07-06 04:48:36.635: ERROR/AndroidRuntime(1884):java.lang.ArrayIndexOutOfBoundsException               
07-06 04:48:36.635: ERROR/AndroidRuntime(1884):     at         android.widget.AbsListView$RecycleBin.addScrapView(AbsListView.java:4528)<br>
07-06 04:48:36.635: ERROR/AndroidRuntime(1884):     at android.widget.ListView.scrollListItemsBy(ListView.java:2863)
07-06 04:48:36.635: ERROR/AndroidRuntime(1884):     at android.widget.ListView.arrowScrollImpl(ListView.java:2340)
07-06 04:48:36.635: ERROR/AndroidRuntime(1884):     at android.widget.ListView.arrowScroll(ListView.java:2287)
07-06 04:48:36.635: ERROR/AndroidRuntime(1884):     at android.widget.ListView.commonKey(ListView.java:2089)
07-06 04:48:36.635: ERROR/AndroidRuntime(1884):     at android.widget.ListView.onKeyDown(ListView.java:2036)
07-06 04:48:36.635: ERROR/AndroidRuntime(1884):     at android.view.KeyEvent.dispatch(KeyEvent.java:1256)
07-06 04:48:36.635: ERROR/AndroidRuntime(1884):     at android.view.View.dispatchKeyEvent(View.java:3855)
07-06 04:48:36.635: ERROR/AndroidRuntime(1884):     at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:787)
07-06 04:48:36.635: ERROR/AndroidRuntime(1884):     at android.widget.ListView.dispatchKeyEvent(ListView.java:2021)
07-06 04:48:36.635: ERROR/AndroidRuntime(1884):     at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:789)
07-06 04:48:36.635: ERROR/AndroidRuntime(1884):     at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:789)
07-06 04:48:36.635: ERROR/AndroidRuntime(1884):     at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:789)
07-06 04:48:36.635: ERROR/AndroidRuntime(1884):     at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchKeyEvent(PhoneWindow.java:1687)
07-06 04:48:36.635: ERROR/AndroidRuntime(1884):     at com.android.internal.policy.impl.PhoneWindow.superDispatchKeyEvent(PhoneWindow.java:1120)
07-06 04:48:36.635: ERROR/AndroidRuntime(1884):     at android.app.Activity.dispatchKeyEvent(Activity.java:2073)
07-06 04:48:36.635: ERROR/AndroidRuntime(1884):     at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:1663)
07-06 04:48:36.635: ERROR/AndroidRuntime(1884):     at android.view.ViewRoot.deliverKeyEventToViewHierarchy(ViewRoot.java:2560)
07-06 04:48:36.635: ERROR/AndroidRuntime(1884):     at android.view.ViewRoot.handleFinishedEvent(ViewRoot.java:2535)
07-06 04:48:36.635: ERROR/AndroidRuntime(1884):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1867)
07-06 04:48:36.635: ERROR/AndroidRuntime(1884):     at android.os.Handler.dispatchMessage(Handler.java:99)
07-06 04:48:36.635: ERROR/AndroidRuntime(1884):     at android.os.Looper.loop(Looper.java:123)
07-06 04:48:36.635: ERROR/AndroidRuntime(1884):     at android.app.ActivityThread.main(ActivityThread.java:3647)
07-06 04:48:36.635: ERROR/AndroidRuntime(1884):     at java.lang.reflect.Method.invokeNative(Native Method)
07-06 04:48:36.635: ERROR/AndroidRuntime(1884):     at java.lang.reflect.Method.invoke(Method.java:507)
07-06 04:48:36.635: ERROR/AndroidRuntime(1884):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
07-06 04:48:36.635: ERROR/AndroidRuntime(1884):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
07-06 04:48:36.635: ERROR/AndroidRuntime(1884):     at dalvik.system.NativeStart.main(Native Method)

我班级的代码是

     import android.app.AlertDialog;
     import android.content.Context;
     import android.view.LayoutInflater;
     import android.view.View;
     import android.view.ViewGroup;
     import android.view.View.OnClickListener;
     import android.widget.ArrayAdapter;
     import android.widget.ImageButton;
     import android.widget.ImageView;
     import android.widget.TextView;
     import android.widget.Toast;

     public class MyClassAdapter extends ArrayAdapter<String> {
 private ArrayList<String> values;
 private LayoutInflater inflator;
     private Context mContext;
 static class ViewHolder {
    public TextView text;
    public TextView gender;
    public TextView distance;
    public ImageView image;
        public ImageView onlineicon;
        public TextView uid;
}

public MyClassAdapter(Context context, ArrayList<String> values) {
    super(context, R.id.TextView01, values);
    this.values = new ArrayList<String>(values);
    inflator = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
   mContext=context;
}

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

@Override
public int getItemViewType(int position) {
    return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View rowView = convertView;

    if (rowView == null) {
        rowView = inflator.inflate(R.layout.row_odd, null);
        ViewHolder viewHolder = new ViewHolder();
        viewHolder.text = (TextView) rowView.findViewById(R.id.TextView01);
        viewHolder.gender=(TextView)rowView.findViewById(R.id.textView2);
        viewHolder.distance=(TextView)rowView.findViewById(R.id.textView1);
        viewHolder.image = (ImageView) rowView
                .findViewById(R.id.ImageView01);
        viewHolder.onlineicon=(ImageView)rowView.findViewById(R.id.imageView1);
        viewHolder.uid=(TextView)rowView.findViewById(R.id.textView3);
        rowView.setTag(viewHolder);
    }
    ViewHolder holder = (ViewHolder) rowView.getTag();
    String Check=values.get(position);

        holder.text.setText(check);
    holder.gender.setText("kss");
    holder.distance.setText("");
    holder.uid.setText("XYz");
    return rowView;
}

}

1 个答案:

答案 0 :(得分:2)

getViewTypeCount中,您的适配器报告正好有2个项目视图类型,但您的getItemViewType方法返回的位置不限于[0,2]范围,除非您只有列表中最多2个项目。每当ListView要求超过秒的位置时,适配器都会报告无效的视图类型。