如何调试Android java.lang.IndexOutOfBoundsException HeaderViewListAdapter.java行

时间:2015-12-28 15:31:54

标签: java android exception

我有一个应用程序,它存放在Android Playstore中,我最近开始看到来自Crashlytics的崩溃报告,其中包含以下内容:

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)

我的应用程序有很多屏幕和视图,从这个跟踪中没有来自我的应用程序的框架:(我如何找出从哪里开始寻找?对此非常感谢。

2 个答案:

答案 0 :(得分:3)

覆盖getHeadersCount()并在数据大小为0时始终返回0.

@Override
public int getHeadersCount(){
  return getCount() == 0 ? 0 : super.getHeaderCount();
}

当您拥有非空数据列表并尝试访问0元素时,会发生这种情况。

此外,您可能需要对isEnabled()方法

执行相同的操作
@Override
public boolean isEnabled(int position){
  return position > 0 || getHeadersCount() > 0 ? super.isEnabled(position) : false;
}

答案 1 :(得分:0)

在使用ListView的自定义适配器时,我遇到了同样的异常。并且从Android标准库类抛出了异常,甚至没有导致我的任何代码行。 我还添加了Header,因此我的适配器被HeaderViewListAdapter隐式包装。 在我的情况下,当我从适配器删除数据时会出现问题。

我认为问题是因为ListView或适配器无法正常使用Header,但实际上原因是其他原因。

我的解决方案是确保永远不会从其他线程更改适配器的数据,并从UI线程调用notifyDataSetChanged()

因此,修复后一切正常,我的代码如下:

// 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;
    }
}