zoukankan      html  css  js  c++  java
  • Android 【问题汇总】列表数组越界的问题

    遇到了一个诡异的问题,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绑定的数据源存在数组越界。

    因为此时,可能会有两个甚至多个线程同时修改数据源。

    导致这个问题的发生。

  • 相关阅读:
    Bootstrap
    继承与多态
    面对对象与封装
    antd表格排序
    样式文本过长用...显示的时候,用弹框来显示文本(react为例)
    锚点
    树形结构的搜索,只显示搜索内容
    fetch不携带cookie
    antd 给select下拉框添加懒加载
    post方法下载文件
  • 原文地址:https://www.cnblogs.com/zhujiabin/p/5859172.html
Copyright © 2011-2022 走看看