android 中的列表视图异常

exception in list view in android

我在列表视图中添加了 header 在过去向上或向下滚动时抛出异常 这是通过自定义视图列表解决的:

    public MyFixedListView(Context context) {
    super(context);
}

public MyFixedListView(Context context, AttributeSet attrs) {
    super(context, attrs);
}

public MyFixedListView(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
}

public MyFixedListView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
    super(context, attrs, defStyleAttr, defStyleRes);
}


@Override
protected void dispatchDraw(Canvas canvas) {
    try {
        super.dispatchDraw(canvas);
    } catch (IndexOutOfBoundsException e) {
        e.printStackTrace();
    }

}

但现在Android 6.0.1 (marshmallow),再次显示是以前的错误: 在我的堆栈跟踪中:

java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0 : at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)

添加 header :

private void Create_Header_Sjd_init() {

    LayoutInflater  inflater = (LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);
    convertViewHeader = inflater.inflate(R.layout.row_idea_list , null);
    convertViewHeader.setBackgroundColor(getResources().getColor(R.color.gray_light_light));

    listView.addHeaderView(convertViewHeader, null, false);
}

IndexOutOfBoundsException,也许您可​​以检查有关数据列表或项目视图的代码

如果您的列表大小 ID 为零,那么为了将 header 添加到列表视图,您已经设置了这个..

listView.setAdapter(null);

通过子代码解决这个错误:

        listView.setFooterDividersEnabled(false);
    listView.addFooterView(new View(this), null, false);