Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.IndexOutOfBoundsException: Invalid index 0, size is 0
   at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:251)
   at java.util.ArrayList.get(ArrayList.java:304)
   at android.widget.HeaderViewListAdapter.isEnabled(HeaderViewListAdapter.java:164)
   at android.widget.ListView.dispatchDraw(ListView.java:3259)
   at android.view.View.draw(View.java:14183)
   at android.widget.AbsListView.draw(AbsListView.java:4960)
   at android.view.View.getDisplayList(View.java:13118)
   at android.view.View.getDisplayList(View.java:13162)
   at android.view.View.draw(View.java:13896)
   at android.view.ViewGroup.drawChild(ViewGroup.java:3024)
   at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2893)
   at android.view.View.draw(View.java:14183)
   at android.widget.FrameLayout.draw(FrameLayout.java:467)
   at android.view.View.getDisplayList(View.java:13118)
   at android.view.View.getDisplayList(View.java:13162)
   at android.view.View.draw(View.java:13896)
   at android.view.ViewGroup.drawChild(ViewGroup.java:3024)
   at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2893)
   at android.view.View.getDisplayList(View.java:13116)
   at android.view.View.getDisplayList(View.java:13162)
   at android.view.View.draw(View.java:13896)
   at android.view.ViewGroup.drawChild(ViewGroup.java:3024)
   at android.support.v4.widget.DrawerLayout.drawChild(DrawerLayout.java:1229)
   at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2893)
   at android.view.View.draw(View.java:14183)
   at android.view.View.getDisplayList(View.java:13118)
   at android.view.View.getDisplayList(View.java:13162)
   at android.view.View.draw(View.java:13896)
   at android.view.ViewGroup.drawChild(ViewGroup.java:3024)
   at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2893)
   at android.view.View.draw(View.java:14183)
   at android.widget.FrameLayout.draw(FrameLayout.java:467)
   at android.view.View.getDisplayList(View.java:13118)
   at android.view.View.getDisplayList(View.java:13162)
   at android.view.View.draw(View.java:13896)
   at android.view.ViewGroup.drawChild(ViewGroup.java:3024)
   at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2893)
   at android.view.View.draw(View.java:14183)
   at android.support.v7.internal.widget.ActionBarOverlayLayout.draw(ActionBarOverlayLayout.java:500)
   at android.view.View.getDisplayList(View.java:13118)
   at android.view.View.getDisplayList(View.java:13162)
   at android.view.View.draw(View.java:13896)
   at android.view.ViewGroup.drawChild(ViewGroup.java:3024)
   at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2893)
   at android.view.View.getDisplayList(View.java:13116)
   at android.view.View.getDisplayList(View.java:13162)
   at android.view.View.draw(View.java:13896)
   at android.view.ViewGroup.drawChild(ViewGroup.java:3024)
   at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2893)
   at android.view.View.getDisplayList(View.java:13116)
   at android.view.View.getDisplayList(View.java:13162)
   at android.view.View.draw(View.java:13896)
   at android.view.ViewGroup.drawChild(ViewGroup.java:3024)
   at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2893)
   at android.view.View.draw(View.java:14183)
   at android.widget.FrameLayout.draw(FrameLayout.java:467)
   at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:2449)
   at android.view.View.getDisplayList(View.java:13118)
   at android.view.View.getDisplayList(View.java:13162)
   at android.view.HardwareRenderer$GlRenderer.draw(HardwareRenderer.java:1198)
   at android.view.ViewRootImpl.draw(ViewRootImpl.java:2431)
   at android.view.ViewRootImpl.performDraw(ViewRootImpl.java:2303)
   at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2109)
   at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1179)
   at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4859)
   at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
   at android.view.Choreographer.doCallbacks(Choreographer.java:562)
   at android.view.Choreographer.doFrame(Choreographer.java:532)
   at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
   at android.os.Handler.handleCallback(Handler.java:725)
   at android.os.Handler.dispatchMessage(Handler.java:92)
   at android.os.Looper.loop(Looper.java:137)
   at android.app.ActivityThread.main(ActivityThread.java:5328)
   at java.lang.reflect.Method.invokeNative(Method.java)
   at java.lang.reflect.Method.invoke(Method.java:511)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
   at dalvik.system.NativeStart.main(NativeStart.java)

My app has got lots of screens and views, and from this trace there is no frame which is from my app :( How do I figure out where to start looking? Any pointers on this very much appreciated.

like image 742
user3570727 Avatar asked Dec 28 '15 15:12

user3570727


2 Answers

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;
}
like image 150
Nikola Despotoski Avatar answered Nov 02 '22 22:11

Nikola Despotoski


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;
    }
}
like image 25
B-GangsteR Avatar answered Nov 02 '22 23:11

B-GangsteR