遇到了一个诡异的问题,ListView发生数组越界(偶尔会),程序崩溃。
错误信息如下:
W/dalvikvm( 5176): threadid=1: thread exiting with uncaught exception (group=0x40015568)
E/zhe800_android( 5176): Invalid index 2, size is 0
E/zhe800_android( 5176): java.lang.IndexOutOfBoundsException: Invalid index 2, size is 0
E/zhe800_android( 5176): at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:257)
E/zhe800_android( 5176): at java.util.ArrayList.get(ArrayList.java:311)
E/zhe800_android( 5176): at android.widget.HeaderViewListAdapter.getView(HeaderViewListAdapter.java:225)
E/zhe800_android( 5176): at android.widget.AbsListView.obtainView(AbsListView.java:1424)
E/zhe800_android( 5176): at android.widget.ListView.makeAndAddView(ListView.java:1755)
E/zhe800_android( 5176): at android.widget.ListView.fillUp(ListView.java:704)
E/zhe800_android( 5176): at android.widget.ListView.fillGap(ListView.java:650)
E/zhe800_android( 5176): at android.widget.AbsListView.trackMotionScroll(AbsListView.java:3422)
E/zhe800_android( 5176): at android.widget.AbsListView$FlingRunnable.run(AbsListView.java:2920)
E/zhe800_android( 5176): at android.os.Handler.handleCallback(Handler.java:587)
E/zhe800_android( 5176): at android.os.Handler.dispatchMessage(Handler.java:92)
E/zhe800_android( 5176): at android.os.Looper.loop(Looper.java:130)
E/zhe800_android( 5176): at android.app.ActivityThread.main(ActivityThread.java:3703)
E/zhe800_android( 5176): at java.lang.reflect.Method.invokeNative(Native Method)
E/zhe800_android( 5176): at java.lang.reflect.Method.invoke(Method.java:507)
E/zhe800_android( 5176): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
E/zhe800_android( 5176): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:624)
E/zhe800_android( 5176): at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime( 5176): FATAL EXCEPTION: main
E/AndroidRuntime( 5176): java.lang.IndexOutOfBoundsException: Invalid index 2, size is 0
E/AndroidRuntime( 5176): at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:257)
E/AndroidRuntime( 5176): at java.util.ArrayList.get(ArrayList.java:311)
E/AndroidRuntime( 5176): at android.widget.HeaderViewListAdapter.getView(HeaderViewListAdapter.java:225)
E/AndroidRuntime( 5176): at android.widget.AbsListView.obtainView(AbsListView.java:1424)
E/AndroidRuntime( 5176): at android.widget.ListView.makeAndAddView(ListView.java:1755)
E/AndroidRuntime( 5176): at android.widget.ListView.fillUp(ListView.java:704)
E/AndroidRuntime( 5176): at android.widget.ListView.fillGap(ListView.java:650)
E/AndroidRuntime( 5176): at android.widget.AbsListView.trackMotionScroll(AbsListView.java:3422)
E/AndroidRuntime( 5176): at android.widget.AbsListView$FlingRunnable.run(AbsListView.java:2920)
E/AndroidRuntime( 5176): at android.os.Handler.handleCallback(Handler.java:587)
E/AndroidRuntime( 5176): at android.os.Handler.dispatchMessage(Handler.java:92)
E/AndroidRuntime( 5176): at android.os.Looper.loop(Looper.java:130)
E/AndroidRuntime( 5176): at android.app.ActivityThread.main(ActivityThread.java:3703)
E/AndroidRuntime( 5176): at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime( 5176): at java.lang.reflect.Method.invoke(Method.java:507)
E/AndroidRuntime( 5176): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
E/AndroidRuntime( 5176): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:624)
E/AndroidRuntime( 5176): at dalvik.system.NativeStart.main(Native Method)
W/ActivityManager( 197): Force finishing activity com.tuan800.tao800/.activities.CategoryDealActivity
从错误日志看,没有定位到具体的问题所在。
跟踪ListView的源码,可以看出来是与Adapter绑定的数据源存在数组越界。
因为此时,可能会有两个甚至多个线程同时修改数据源。
导致这个问题的发生。
参考资料
http://bbs.csdn.net/topics/390775554
http://blog.csdn.net/tw19811220/article/details/9139595
http://blog.csdn.net/l_serein/article/details/7706338
http://www.chengxuyuans.com/Android/63263.html
http://hi.baidu.com/guoxiaoming/item/7b337449864f3fe11381dacd