简体   繁体   中英

java.lang.IndexOutOfBoundsException: Index: 2, Size: 0

This error happens sporadically when I draw canvas from view

  val view: View = this.activity.getWindow().getDecorView().getRootView()

        val bitmap = Bitmap.createBitmap(
            view.width,
            view.height,
            Bitmap.Config.ARGB_8888
        ) // Bitmap()


        val canvas = Canvas(bitmap)
        view.draw(canvas) // trace brings me here

Here is the rest of the log

  java.lang.IndexOutOfBoundsException: Index: 2, Size: 0
        at java.util.ArrayList.get(ArrayList.java:437)
        at android.view.ViewGroup.getAndVerifyPreorderedView(ViewGroup.java:3624)
        at android.view.ViewGroup.dispatchDraw(ViewGroup.java:4110)
        at android.view.View.draw(View.java:20075)
        at android.view.ViewGroup.drawChild(ViewGroup.java:4333)
        at android.view.ViewGroup.dispatchDraw(ViewGroup.java:4112)
        at android.view.View.draw(View.java:20075)
        at android.view.ViewGroup.drawChild(ViewGroup.java:4333)
        at android.view.ViewGroup.dispatchDraw(ViewGroup.java:4112)
        at android.view.View.draw(View.java:20075)
        at android.view.ViewGroup.drawChild(ViewGroup.java:4333)
        at android.view.ViewGroup.dispatchDraw(ViewGroup.java:4112)
        at android.view.View.draw(View.java:20075)
        at android.view.ViewGroup.drawChild(ViewGroup.java:4333)
        at android.view.ViewGroup.dispatchDraw(ViewGroup.java:4112)
        at android.view.View.draw(View.java:20075)
        at android.view.ViewGroup.drawChild(ViewGroup.java:4333)
        at android.view.ViewGroup.dispatchDraw(ViewGroup.java:4112)
        at android.view.View.draw(View.java:20210)
        at com.android.internal.policy.DecorView.draw(DecorView.java:790)

this kind of error (exception) is NOT from code that u wrote up there. this kind of errors java.lang.IndexOutOfBoundsException: Index: 2, Size: 0 is just for ArrayList or Array which means if we have an ArrayList with x index and we call an index more than x , this error will be raised!

so what is the problem?

the problem is : u have an ArrayList that u are calling more than ArrayList indexes!

how u can solve it?

run your program in android studio, and when this exception rasied click on excpetion line... android studio will goes the current line that has bug, so u can see your ArrayList there and check the indexes.

and one more thing, i am NOT saying the upper code is correct, maybe it is and maybe it is not! BUT i am sure that before this lines called, another lines(your ArrayList code lines) called and this problem is for your ArrayList code lines.

so u have to fix this bug first, and then go to the next lines...

good luck

Try calling your function from main thread - seems to have fixed it for me. I think the issue is iterating the view hierarchy off of main thread, and then some main thread caller is modifying the view hierarchy in some way, occasionally at the same time you are iterating.

If you hold the main thread, then the hierarchy cannot be modified as you iterate it, and so you will not encounter this issue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM