ListView random IndexOutOfBoundsException on Froyo

前端 未结 2 1044
无人共我
无人共我 2020-12-03 01:16

I have an app with tons of downloads and I\'m receiving a lot of this error:

 16783         AndroidRuntime  E  java.lang.IndexOutOfBoundsException: Invalid i         


        
相关标签:
2条回答
  • 2020-12-03 02:07

    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.

    0 讨论(0)
  • 2020-12-03 02:17

    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();
        }
    }
    
    0 讨论(0)
提交回复
热议问题