数组越界,但是Log定位不到问题

最近在调试程序时,发现程序有很老的版本升级到新版本后,跳转到Fragment界面,一直崩溃,下面是崩溃日志:

java.lang.IndexOutOfBoundsException: Invalid index 2, size is 2
    at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
    at java.util.Arrays$ArrayList.get(Arrays.java:66)
    at android.widget.ArrayAdapter.getItem(ArrayAdapter.java:337)
    at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:390)
    at android.widget.ArrayAdapter.getView(ArrayAdapter.java:362)
    at android.widget.Spinner.makeView(Spinner.java:617)
    at android.widget.Spinner.layout(Spinner.java:565)
    at android.widget.Spinner.onLayout(Spinner.java:527)
    at android.view.View.layout(View.java:15806)
    at android.view.ViewGroup.layout(ViewGroup.java:5038)
    at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1703)
    at android.widget.LinearLayout.layoutHorizontal(LinearLayout.java:1692)
    at android.widget.LinearLayout.onLayout(LinearLayout.java:1468)
    at android.view.View.layout(View.java:15806)
    at android.view.ViewGroup.layout(ViewGroup.java:5038)
    at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1703)
    at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1557)
    at android.widget.LinearLayout.onLayout(LinearLayout.java:1466)
    at android.view.View.layout(View.java:15806)
    at android.view.ViewGroup.layout(ViewGroup.java:5038)
    at android.widget.FrameLayout.layoutChildren(FrameLayout.java:581)
    at android.widget.FrameLayout.onLayout(FrameLayout.java:516)
    at android.widget.ScrollView.onLayout(ScrollView.java:1502)
    at android.view.View.layout(View.java:15806)
    at android.view.ViewGroup.layout(ViewGroup.java:5038)
    at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1703)
    at android.widget.LinearLayout.layoutHorizontal(LinearLayout.java:1692)
    at android.widget.LinearLayout.onLayout(LinearLayout.java:1468)
    at android.view.View.layout(View.java:15806)
    at android.view.ViewGroup.layout(ViewGroup.java:5038)
    at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1076)
    at android.view.View.layout(View.java:15806)
    at android.view.ViewGroup.layout(ViewGroup.java:5038)
    at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1076)
    at android.view.View.layout(View.java:15806)
    at android.view.ViewGroup.layout(ViewGroup.java:5038)
    at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1076)
    at android.view.View.layout(View.java:15806)
    at android.view.ViewGroup.layout(ViewGroup.java:5038)
    at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1076)
    at android.view.View.layout(View.java:15806)
    at android.view.ViewGroup.layout(ViewGroup.java:5038)
    at android.widget.FrameLayout.layoutChildren(FrameLayout.java:581)
    at android.widget.FrameLayout.onLayout(FrameLayout.java:516)
    at android.view.View.layout(View.java:15806)
    at android.view.ViewGroup.layout(ViewGroup.java:5038)
    at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1703)
    at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1557)
    at android.widget.LinearLayout.onLayout(LinearLayout.java:1466)
    at android.view.View.layout(View.java:15806)
    at android.view.ViewGroup.layout(ViewGroup.java:5038)
    at android.widget.FrameLayout.layoutChildren(FrameLayout.java:581)
    at android.widget.FrameLayout.onLayout(FrameLayout.java:516)
    at android.view.View.layout(View.java:15806)
    at android.view.ViewGroup.layout(ViewGroup.java:5038)
    at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1703)
    at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1557)
    at android.widget.LinearLayout.onLayout(LinearLayout.java:1466)
    at android.view.View.layout(View.java:15806)
    at android.view.ViewGroup.layout(ViewGroup.java:5038)
    at android.widget.FrameLayout.layoutChildren(FrameLayout.java:581)
    at android.widget.FrameLayout.onLayout(FrameLayout.java:516)
    at android.view.View.layout(View.java:15806)
    at android.view.ViewGroup.layout(ViewGroup.java:5038)
    at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2202)
    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1959)
    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1161)
    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6198)
    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767)
    at android.view.Choreographer.doCallbacks(Choreographer.java:580)
    at android.view.Choreographer.doFrame(Choreographer.java:550)
    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753)
    at android.os.Handler.handleCallback(Handler.java:739)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:135)
    at android.app.ActivityThread.main(ActivityThread.java:5280)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:963)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:758)

Log里面没有定位到具体的代码块,都是数组控件类的错误,当时程序代码很多,当时初步定位到的是从数据库查找到数据后填入Adapter导致的数组越界,后来Debug发现没有问题。

再后来就是以注释代码的方式找出出问题的代码,终于定位到了一个私有方法,于是开始Debug,结果全部执行下来没问题,加上try catch也捕获不到。最后实在没办法了,确定是这个方法的问题后,只能一行一行的找问题,既然是跟数组有关,那要么就是List,要么就是【】数组,最后还是通过注释代码的方式定位到那行错误的代码:

mSpnContinueWayAll.setSelection(continueNo);

这是一个Spinner选择器,由于老版本程序的String-array和现在的不一样,导致出错。出错不要紧,但是没办法定位就很难受了。对代码熟悉的可能定位就很快,但是代码很多又不熟悉的就不好弄了。

    原文作者:andy_XJ
    原文地址: https://www.jianshu.com/p/76b7098f1d95
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞