【问题标题】:Android Adapter crashing with ArrayIndexOutOfBoundsExceptionAndroid 适配器因 ArrayIndexOutOfBoundsException 而崩溃
【发布时间】:2016-12-07 04:26:35
【问题描述】:

我了解此异常描述了我们试图访问不存在/超过数组大小的元素的情况。但是,就我而言,我无法准确指出其来源。

这个异常很生动,没有描述任何可能引起问题的代码行。

适配器内部还有一个过滤器,因为我需要一个搜索功能来过滤数组列表中的项目

适配器类:

public class SearchAdapter extends BaseAdapter {

    private Activity activity;
    private static LayoutInflater inflater = null;
    ArrayList<Event> resultList = new ArrayList<>();
    private ArrayList<Event> arraylist = null;

    public SearchAdapter(Activity context, ArrayList<Event> resultList) {
        this.activity = context;
        this.resultList = resultList;
        this.inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        this.arraylist = new ArrayList<Event>();
        this.arraylist.addAll(resultList);
    }

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

    @Override
    public Event getItem(int i) {
        return resultList.get(i);
    }

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

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

    @Override
    public int getItemViewType(int position) {
        if (position < resultList.size())
            return 0;
        return 1;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup viewGroup) {

        View view = convertView;
        Holder holder = new Holder();
        final Event result = getItem(position);

        if (view == null) {
            view = inflater.inflate(R.layout.row_single_search_row, null);

            holder.parent = (RelativeLayout) view.findViewById(R.id.parent);
            holder.name = (TextView) view.findViewById(R.id.name);
            holder.details = (TextView) view.findViewById(R.id.details);

            view.setTag(holder);

        } else {
            holder = (Holder) view.getTag();
        }

        holder.name.setText(result.getTitle());
        holder.details.setText(result.getDescriptions());

        return view;
    }

    public static class Holder {
        RelativeLayout parent;
        TextView name, details;
    }

    //I believe this 'might' be where the problem originates
    //This method is used to act as a filter that filters/searches items when text is entered to a EditText in the main activity
    public void filter(String charText) {
        charText = charText.toLowerCase(Locale.getDefault());
        resultList.clear();
        if (charText.length() == 0) {
            resultList.addAll(arraylist);
        } else {
            for (Event wp : arraylist) {
                if (wp.getTitle().toLowerCase(Locale.getDefault()).contains(charText)) {
                    resultList.add(wp);
                }
            }
        }
        notifyDataSetChanged();
    }

}

主 Activity 的 OnCreate:

searchBox.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

                if (adapter != null && searchListView.getAdapter() != null) {
                    adapter.filter(s.toString());
                }
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                                          int after) {

            }

            @Override
            public void afterTextChanged(Editable s) {
            }
 });

我得到的错误:

java.lang.ArrayIndexOutOfBoundsException: length=3; index=3
    at android.widget.AbsListView$RecycleBin.addScrapView(AbsListView.java:8948)
    at android.widget.ListView.layoutChildren(ListView.java:1681)
    at android.widget.AbsListView.onLayout(AbsListView.java:2723)
    at android.view.View.layout(View.java:17938)
    at android.view.ViewGroup.layout(ViewGroup.java:5812)
    at android.support.v4.widget.SwipeRefreshLayout.onLayout(SwipeRefreshLayout.java:613)
    at android.view.View.layout(View.java:17938)
    at android.view.ViewGroup.layout(ViewGroup.java:5812)
    at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1080)
    at android.view.View.layout(View.java:17938)
    at android.view.ViewGroup.layout(ViewGroup.java:5812)
    at android.support.v4.view.ViewPager.onLayout(ViewPager.java:1767)
    at android.view.View.layout(View.java:17938)
    at android.view.ViewGroup.layout(ViewGroup.java:5812)
    at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1080)
    at android.view.View.layout(View.java:17938)
    at android.view.ViewGroup.layout(ViewGroup.java:5812)
    at android.widget.FrameLayout.layoutChildren(FrameLayout.java:344)
    at android.widget.FrameLayout.onLayout(FrameLayout.java:281)
    at android.view.View.layout(View.java:17938)
    at android.view.ViewGroup.layout(ViewGroup.java:5812)
    at android.support.v7.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:433)
    at android.view.View.layout(View.java:17938)
    at android.view.ViewGroup.layout(ViewGroup.java:5812)
    at android.widget.FrameLayout.layoutChildren(FrameLayout.java:344)
    at android.widget.FrameLayout.onLayout(FrameLayout.java:281)
    at android.view.View.layout(View.java:17938)
    at android.view.ViewGroup.layout(ViewGroup.java:5812)
    at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1742)
    at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1585)
    at android.widget.LinearLayout.onLayout(LinearLayout.java:1494)
    at android.view.View.layout(View.java:17938)
    at android.view.ViewGroup.layout(ViewGroup.java:5812)
    at android.widget.FrameLayout.layoutChildren(FrameLayout.java:344)
    at android.widget.FrameLayout.onLayout(FrameLayout.java:281)
    at com.android.internal.policy.PhoneWindow$DecorView.onLayout(PhoneWindow.java:3178)
    at android.view.View.layout(View.java:17938)
    at android.view.ViewGroup.layout(ViewGroup.java:5812)
    at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2666)
    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2367)
    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1437)
    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:7397)
    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:920)
    at android.view.Choreographer.doCallbacks(Choreographer.java:695)
    at android.view.Choreographer.doFrame(Choreographer.java:631)
    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:906)
    at android.os.Handler.handleCallback(Handler.java:739)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:158)
    at android.app.ActivityThread.main(ActivityThread.java:7225)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)

【问题讨论】:

  • 查看resultList数组大小?
  • 你能用调试器指向位置吗??
  • 是的,问题出在您的过滤方法上。请在 filter 方法中添加 try catch 块并进行一些检查,我会建议在不使用 filter 方法的情况下填充列表并让我们知道。 ?
  • @sasikumar 这不是空的。检查了多次。当我尝试在editext中搜索时出现问题。哪个指向Filter方法
  • @DinukaJayasuriya :您能指出异常是在哪一行以及何时触发的吗?

标签: java android listview android-adapter


【解决方案1】:

我认为你在这里得到了错误的列表

@Override
public Event getItem(int i) {
    return resultList.get(i);
}

确定你真正需要的清单

ArrayList<Event> resultList = new ArrayList<>();
private ArrayList<Event> arraylist = null;

因为你只加载其中一个

this.arraylist = new ArrayList<Event>();
this.arraylist.addAll(resultList);

换句话说,小心这个。

this.resultList = resultList;

每当您在此适配器外部编辑该列表时,您都在编辑适配器内的相同引用,这可能会导致您的错误

【讨论】:

    【解决方案2】:

    您没有在arraylist 中正确分配值。使用

     this.arraylist = new ArrayList<>(resultList);
    

    而不是

     this.arraylist = new ArrayList<Event>();
    

    删除 this.arraylist.addAll(resultList);

    【讨论】:

    • 可能是。但是,在这种情况下它应该显示 NullPointerException 吗?
    【解决方案3】:
    You can change in your filter method like this
    if(charText==0){
        this.resultList=arrayList; //that is your original list
    }else{
        resultList = new ArrayList<Event>();
        for (Event wp : arraylist) {
                        if (wp.getTitle().toLowerCase(Locale.getDefault()).contains(charText)) {
                            resultList.add(wp);
                        }
                    }
    this.resultList=resultList;
        }
    notifyDataSetChanged();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-07
      • 2016-04-23
      • 1970-01-01
      • 2013-11-15
      • 1970-01-01
      相关资源
      最近更新 更多