2016-12-07 79 views
-1

據我所知,這個異常描述了一種情況,我們試圖訪問一個不存在/超過數組大小的元素。然而就我而言,我無法準確指出這起源於哪裏。Android適配器崩潰與ArrayIndexOutOfBoundsException

這個例外很生動,並沒有描述問題可能出現的任何代碼行。

我也有適配器內部的過濾器,因爲我需要一個搜索功能ArrayList中篩選項目

適配器類:

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

} 

主要活動的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) 
+0

檢查resultList數組的大小? – sasikumar

+0

你能指出使用調試器的位置嗎? –

+0

是的,問題出在你的過濾方法中。請在過濾方法中添加try catch塊,並進行一些檢查, 那麼我會推薦使用過濾方法填充列表,並讓我們知道。 ? –

回答

0

您在arraylist中沒有正確賦值。使用

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

,而不是

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

刪除this.arraylist.addAll(resultList);

+0

可能是。但是,在這種情況下它應該顯示NullPointerException? – Vinodh

1

我覺得你在這裏

​​

得到錯誤的清單確定哪些列表,你真的需要

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

因爲你只能加載其中之一

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

換句話說,小心這個。

this.resultList = resultList; 

每當你從外部編輯列表,該適配器外,您正在編輯的適配器中相同的參考,這可能導致你的錯誤

0
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();