Skip to content Skip to sidebar Skip to footer

How To Debug Android Java.lang.indexoutofboundsexception Headerviewlistadapter.java Line

I have an app which is live in the android playstore, where I recently started seeing crash reports from Crashlytics with the following trace : Fatal Exception: java.lang.IndexOut

Solution 1:

Override getHeadersCount() and always return 0 when you data size is 0.

@OverridepublicintgetHeadersCount(){
  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

@OverridepublicbooleanisEnabled(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@OverridepublicLoader<...> onCreateLoader(int id, Bundle args) {
    returnnewLoader...
}

@OverridepublicvoidonLoadFinished(Loader<...> loader, Data data) {
    ...
    // adapter's list of items
    listItems.clear();
    listItems.addAll(data);
    adapter.notifyDataSetChanged();
    ...
}

@OverridepublicvoidonLoaderReset(Loader<List<? extendsMap<String, ?>>> loader) {
    ...
}

// custom loader privatestaticclassContactAsyncLoaderextendsAsyncTaskLoader<...> {
    @OverridepublicList<..> loadInBackground() {
        Data data = new ..
        ...
        return data;
    }
}

Post a Comment for "How To Debug Android Java.lang.indexoutofboundsexception Headerviewlistadapter.java Line"