Skip to content Skip to sidebar Skip to footer

Indexoutofboundsexception With Listview

I just got a strange indexoutofboundsexception that I can't seem to reproduce. It seems to be related to my listactivity: java.lang.IndexOutOfBoundsException: Invalid index 1, size

Solution 1:

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 the position is larger than listview thought it will be.

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

Solution 2:

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!

Solution 3:

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

Solution 4:

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

Solution 5:

I met the same issue when using ListView with header view or footer view to add/remove. So tried different approaches on Stack Overflow, I found a workaround solution to ignore these errors which to handle the dispatchDraw method as below.

publicclassCustomListViewextendsListView{

   @OverrideprotectedvoiddispatchDraw(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@.@

Post a Comment for "Indexoutofboundsexception With Listview"