ListView random IndexOutOfBoundsException on Froyo

回眸只為那壹抹淺笑 提交于 2019-11-27 12:46:31
Draiken

After a lot of time checking the android source code and not understanding this error I've finally cracked it.

The problem occurs with Samsung phones, they have a different implementation on the over-scroll functionality and that ended up throwing this exception as it tried to select a footer/header out of bounds (even when there is no footer view).

The solution I used is not pretty but it will stop this error from happening ever again.

class MyFixedListView extends ListView {
    @Override
    protected void dispatchDraw(Canvas canvas) {
        try {
            super.dispatchDraw(canvas);
        } catch (IndexOutOfBoundsException e) {
            // samsung error
        }
    }
}

Now I use this ListView implementation and the error is gone.

I really hope this helps anyone using endless adapters.

Denys Nikolayenko

Same issue happens in my app when testing on the ICS tablets. It happens only when I have 11 elements in the list, when EndlessAdapter loads by 10 items at time.

This is a race condition issue. The problem comes from the keepOnAppending flag update on the AsyncTask thread. While ListView accesses getCount on the UI thread andkeepOnAppending is set to true, later when it calls getView in the same UI thread andkeepOnAppending is already reset in the doInBackground:

@Override
public int getCount() {
    if (keepOnAppending.get()) {
        return (super.getCount() + 1); // one more for
                                        // "pending"
    }

    return (super.getCount());
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    if (position == super.getCount() && keepOnAppending.get()) {
        if (pendingView == null) {
            pendingView = getPendingView(parent);

            executeAsyncTask(new AppendTask());
        }

        return (pendingView);
    }
    return (super.getView(position, convertView, parent));
}

class AppendTask extends AsyncTask<Void, Void, Exception> {
    @Override
    protected Exception doInBackground(Void... params) {
        Exception result = null;

        try {
            keepOnAppending.set(cacheInBackground());
        } catch (Exception e) {
            result = e;
        }

        return (result);
    }

    @Override
    protected void onPostExecute(Exception e) {
        if (e == null) {
            appendCachedData();
        } else {
            keepOnAppending.set(onException(pendingView, e));
        }

        pendingView = null;
        notifyDataSetChanged();
    }
}

As a fix, I changed the code of the AppendTask so that it updates keepOnAppending on the UI thread, only when new items added to the adapter.

Here is the code:

class AppendTask extends AsyncTask<Void, Void, AppendTask.Result> {
    class Result {
        boolean status;
        Exception ex;

        public Result(Exception ex) {
            this.ex = ex;
        }

        public Result(boolean result) {
            this.status = result;
        }
    }

    @Override
    protected AppendTask.Result doInBackground(Void... params) {
        try {
            return new Result(cacheInBackground());
        } catch (Exception e) {
            return new Result(e);
        }
    }

    @Override
    protected void onPostExecute(AppendTask.Result result) {
        if (result.ex == null) {
            appendCachedData();
            keepOnAppending.set(result.status);
        } else {
            keepOnAppending.set(onException(pendingView, result.ex));
        }

        pendingView = null;
        notifyDataSetChanged();
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!