ListView random IndexOutOfBoundsException on Froyo
来源:互联网 发布:如果国民党赢了 知乎 编辑:程序博客网 时间:2024/05/17 23:54
http://stackoverflow.com/questions/8431342/listview-random-indexoutofboundsexception-on-froyo[/mw_shl_code]
ListView random IndexOutOfBoundsException on Froyo
I have an app with tons of downloads and I'm receiving a lot of this error:
16783 AndroidRuntime E java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0 16783 AndroidRuntime E at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:257) 16783 AndroidRuntime E at java.util.ArrayList.get(ArrayList.java:311) 16783 AndroidRuntime E at android.widget.HeaderViewListAdapter.isEnabled(HeaderViewListAdapter.java:16 4) 16783 AndroidRuntime E at android.widget.ListView.dispatchDrawWithExcessScroll_Default(ListView.java:3 288) 16783 AndroidRuntime E at android.widget.ListView.dispatchDraw(ListView.java:3029) 16783 AndroidRuntime E at android.view.View.draw(View.java:6743) 16783 AndroidRuntime E at android.widget.AbsListView.draw(AbsListView.java:2549) 16783 AndroidRuntime E at android.view.ViewGroup.drawChild(ViewGroup.java:1640) 16783 AndroidRuntime E at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1367) 16783 AndroidRuntime E at android.view.View.draw(View.java:6743) 16783 AndroidRuntime E at android.view.ViewGroup.drawChild(ViewGroup.java:1640) 16783 AndroidRuntime E at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1367) 16783 AndroidRuntime E at android.view.ViewGroup.drawChild(ViewGroup.java:1638) 16783 AndroidRuntime E at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1367) 16783 AndroidRuntime E at android.view.ViewGroup.drawChild(ViewGroup.java:1638) 16783 AndroidRuntime E at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1367) 16783 AndroidRuntime E at android.view.ViewGroup.drawChild(ViewGroup.java:1638) 16783 AndroidRuntime E at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1367) 16783 AndroidRuntime E at android.view.ViewGroup.drawChild(ViewGroup.java:1638) 16783 AndroidRuntime E at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1367) 16783 AndroidRuntime E at android.view.ViewGroup.drawChild(ViewGroup.java:1638) 16783 AndroidRuntime E at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1367) 16783 AndroidRuntime E at android.view.ViewGroup.drawChild(ViewGroup.java:1638) 16783 AndroidRuntime E at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1367) 16783 AndroidRuntime E at android.view.ViewGroup.drawChild(ViewGroup.java:1638) 16783 AndroidRuntime E at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1367) 16783 AndroidRuntime E at android.view.View.draw(View.java:6743) 16783 AndroidRuntime E at android.view.ViewGroup.drawChild(ViewGroup.java:1640) 16783 AndroidRuntime E at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1367) 16783 AndroidRuntime E at android.view.ViewGroup.drawChild(ViewGroup.java:1638) 16783 AndroidRuntime E at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1367) 16783 AndroidRuntime E at android.view.View.draw(View.java:6743) 16783 AndroidRuntime E at android.widget.FrameLayout.draw(FrameLayout.java:352) 16783 AndroidRuntime E at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java :1885) 16783 AndroidRuntime E at android.view.ViewRoot.draw(ViewRoot.java:1407) 16783 AndroidRuntime E at android.view.ViewRoot.performTraversals(ViewRoot.java:1163) 16783 AndroidRuntime E at android.view.ViewRoot.handleMessage(ViewRoot.java:1727) 16783 AndroidRuntime E at android.os.Handler.dispatchMessage(Handler.java:99) 16783 AndroidRuntime E at android.os.Looper.loop(Looper.java:123) 16783 AndroidRuntime E at android.app.ActivityThread.main(ActivityThread.java:4627) 16783 AndroidRuntime E at java.lang.reflect.Method.invokeNative(Native Method) 16783 AndroidRuntime E at java.lang.reflect.Method.invoke(Method.java:521) 16783 AndroidRuntime E at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:8 58) 16783 AndroidRuntime E at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 16783 AndroidRuntime E at dalvik.system.NativeStart.main(Native Method)
As you can see in the stack trace, there is not one line with my code trace in it. To reproduce this, I luckily found an user with a Froyo (2.2 p7) and I just scrolled down one of the ListView
s on the code. After some random time, it just froze with this exception. This happens every time at a different time.
It is a ListView
with an EndlessAdapter
behind it, just adding more and more rows. Apparently I get this error when I do the "over-scroll" but I cannot even think of a way to fix this. It is hard enough to reproduce, but with almost 200 users online at every time, if they keep getting this error they will end up not using the app anymore.
Any help would be appreciated.
EDIT: There has been a similar issue with someone else with the EndlessAdapter
.http://groups.google.com/group/cw-android/browse_thread/thread/4739ce05742841da/af59c779e99f5e23?lnk=gst&q=index#af59c779e99f5e23
But it's not EndlessAdapter
's fault. It's android's fault.
3 Answers
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.
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:
@Overridepublic int getCount() { if (keepOnAppending.get()) { return (super.getCount() + 1); // one more for // "pending" } return (super.getCount());}@Overridepublic 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(); }}
ListView
with their own class and that generated the error. – Draiken Jul 13 '12 at 11:51 Do you have a footer view in your list? It seems that it is not ready sometimes, when you scroll down.
Also this might help:
Indexoutofboundsexception with listview
- ListView random IndexOutOfBoundsException on Froyo
- android listview setOnItemClickListener IndexOutOfBoundsException
- Apps on SD Card on Froyo
- Apps on SD Card on Froyo
- java.lang.NoSuchMethodError: smoothScrollToPositionFromTop for Froyo ListView?
- IndexOutOfBoundsException
- Android知识总结:ListView 出现IndexOutOfBoundsException异常
- Android ListView IndexOutOfBoundsException ViewHolder类型转化错误
- Compile ASOP Froyo for Dream on Ubuntu 10.4
- Install Android 2.2 Froyo on Nexus One [For Rooted Users]
- Install Android 2.2.1 Froyo On Nook Color [Advanced Guide]
- android.intent.action.CAMERA_BUTTON not broadcasting on Desire Z (Froyo)?
- 点击listview item 报 java.lang.IndexOutOfBoundsException 问题解决方案分析
- Listview indexOutOfBoundsException: Invalid index 1, size is 1
- Random thoughts on code review
- Random Walks on the Click Graph
- ListView 点击加载更多出现异常(java.lang.IndexOutOfBoundsException: Invalid index 10, size is 10)
- Listview添加完HeaderView或者FooterView后,点击相应的view出现IndexOutOfBoundsException问题修复
- Java 随机数
- hive中子查询实例
- 动画(五)属性动画的实现原理
- GOF共23种设计模式
- pat 1079 Total Sales of Supply Chain
- ListView random IndexOutOfBoundsException on Froyo
- 基于display:table的CSS布局让HTML元素和像table一样
- iOS面试题非技术面试(二)
- C++开发者都应该使用的10个C++11特性
- 在Android Studio添加SO library
- C++ 中的回调函数
- 动画(六)属性动画的工作原理
- Html5 本地存储
- Android还在用Toast?你Out啦,该试试Snackbar了
ListView.smoothScrollToPosition(...)
– DoriJan 22 '14 at 15:12