滚动列表视图时出现错误/AndroidRuntime(1884): java.lang.ArrayIndexOutOfBoundsException

发布于 2024-11-18 12:39:27 字数 5893 浏览 3 评论 0原文

您好,我在列表视图中使用自定义适配器类作为适配器,当我在大约 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;
}

}

Hi I am using a custom Adapter class as an adapter in listview when I scroll the list after around 5-6 positions my application force closes on looking at the LogCat output I get

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)

The code for my class is

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

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ゝ杯具 2024-11-25 12:39:27

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

In getViewTypeCount your adapter is reporting that there are exactly 2 item view types, but your getItemViewType method returns the position which is not restricted to the range [0, 2) unless you only have a maximum of 2 items in your list. Whenever ListView asks for a position beyond the second, your adapter reports an invalid view type.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文