How To Debug Android Java.lang.IndexOutOfBoundsException HeaderViewListAdapter.java Line
Solution 1:
Override getHeadersCount()
and always return 0 when you data size is 0.
@Override
public int getHeadersCount(){
return getCount() == 0 ? 0 : super.getHeaderCount();
}
This happens when you are having non null data list and it attempts to access 0 element.
Also you might need to do the same with isEnabled()
method
@Override
public boolean isEnabled(int position){
return position > 0 || getHeadersCount() > 0 ? super.isEnabled(position) : false;
}
Solution 2:
I was getting the same exception while using custom adapter with ListView. And exception was thrown from Android standard library classes, not even leading to any line of my code. I also was adding Header, so my adapter was implicitly wrapped by HeaderViewListAdapter. In my case the problem appears when I'm deleting data from adapter.
I was thinking that the problem is because ListView or adapter can't work fine with Header, but for real the reason was other.
My solution is to make sure that adapter's data is never changed from other threads, and notifyDataSetChanged()
is called from UI thread.
So, after fixes everything works and my code looks like:
// Loader's callbacks
@Override
public Loader<...> onCreateLoader(int id, Bundle args) {
return new Loader...
}
@Override
public void onLoadFinished(Loader<...> loader, Data data) {
...
// adapter's list of items
listItems.clear();
listItems.addAll(data);
adapter.notifyDataSetChanged();
...
}
@Override
public void onLoaderReset(Loader<List<? extends Map<String, ?>>> loader) {
...
}
// custom loader
private static class ContactAsyncLoader extends AsyncTaskLoader<...> {
@Override
public List<..> loadInBackground() {
Data data = new ..
...
return data;
}
}
Post a Comment for "How To Debug Android Java.lang.IndexOutOfBoundsException HeaderViewListAdapter.java Line"