Indexoutofboundsexception with listview

后端 未结 5 338
抹茶落季
抹茶落季 2020-12-21 06:28

I just got a strange indexoutofboundsexception that I can\'t seem to reproduce. It seems to be related to my listactivity:

java.lang.IndexOutOfBoundsExceptio         


        
相关标签:
5条回答
  • 2020-12-21 06:52

    Use addFooterView(footerView) it will be solve this problem.

    0 讨论(0)
  • 2020-12-21 06:53

    @Kevin Qiu the hint to hide stuff helped. I only had to hide the headerview, not the whole list. Here's the code that worked for me, assuming that the header view is named mHeaderView
    @Override public void onStop() { super.onStop(); mHeaderView.setVisibility(View.GONE);// fixes weird bug with header view }

    And to Make sure it shows up after you switch to a different app, do the code below.
    @Override public void onStart() { if(mHeaderView.getVisibility() == View.GONE) mHeaderView.setVisibility(View.VISIBLE); super.onStart(); }

    0 讨论(0)
  • 2020-12-21 06:55

    After looking through the code on the google site, I see a lot of different types of initializations and accesses to the mHeaderViewInfos, but I don't actually see anything being added to it. It might benefit to throw in some debug points and trace it through. Good luck!

    0 讨论(0)
  • 2020-12-21 07:04

    I met the same issue when use ListView with header view or footer view to add/remove. So tried different approaches on Stack Overflow, I found a work around solutions to ignore this errors which to handle dispatchDraw method as below.

    public class CustomListView extends ListView{
    
       @Override
       protected void dispatchDraw(Canvas canvas) {
          try {
              super.dispatchDraw(canvas);
          } catch (IndexOutOfBoundsException e) {
              Log.e("luna", "Ignore list view error ->" + e.toString());
          }
       }
    }
    

    Hope that can help u.

    Please refer to this link How to debug Android java.lang.IndexOutOfBoundsException HeaderViewListAdapter.java line

    Have Fun@.@

    0 讨论(0)
  • 2020-12-21 07:09

    I solve this problem by make sure notify adapter when data set is changed. In my case,data was changed by another thread,after listview check if data was right. After look into listview source. I fount the exception throw out here:

        public View More ...getView(int position, View convertView, ViewGroup parent) {
        // Header (negative positions will throw an ArrayIndexOutOfBoundsException)
        int numHeaders = getHeadersCount();
        if (position < numHeaders) {
            return mHeaderViewInfos.get(position).view;
        }
    
        // Adapter
        final int adjPosition = position - numHeaders;
        int adapterCount = 0;
        if (mAdapter != null) {
            adapterCount = mAdapter.getCount();
            if (adjPosition < adapterCount) {
                return mAdapter.getView(adjPosition, convertView, parent);
            }
        }
    
        // Footer (off-limits positions will throw an ArrayIndexOutOfBoundsException)
        return mFooterViewInfos.get(adjPosition - adapterCount).view;
    }
    

    So you can see that mFooterViewInfos.get(adjPosition - adapterCount).view throw a Indexoutofboundsexception. Because position is larger than listview thought it will be.

    What you have to do is find out whitch view cause this exception. And make sure once the data changed notify the adapter immediately!

    0 讨论(0)
提交回复
热议问题