简体   繁体   English

java.lang.IndexOutOfBoundsException:索引:2,大小:0

[英]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!这种错误java.lang.IndexOutOfBoundsException: Index: 2, Size: 0仅用于ArrayListArray这意味着如果我们有一个带有x索引的ArrayList并且我们调用的索引大于x ,则会引发此错误!

so what is the problem?那么问题是什么?

the problem is : u have an ArrayList that u are calling more than ArrayList indexes!问题是:您有一个ArrayList ,您调用的不仅仅是ArrayList索引!

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.在 android studio 中运行你的程序,当这个异常出现时,点击 excpetion 行...android studio 将转到有错误的当前行,所以你可以在那里看到你的ArrayList并检查索引。

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.但我确信在调用此行之前,调用了另一行(您的ArrayList代码行),这个问题是针对您的ArrayList代码行。

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.如果您持有主线程,则在您迭代它时无法修改层次结构,因此您不会遇到此问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 java.lang.IndexOutOfBoundsException:索引:1,大小:1 - java.lang.IndexOutOfBoundsException: Index: 1, Size: 1 java.lang.IndexOutOfBoundsException:索引:1,大小:1 onBindViewHolder - java.lang.IndexOutOfBoundsException: Index: 1, Size: 1 onBindViewHolder java.lang.IndexOutOfBoundsException: Index: 1, Size: 1 in Kotlin - java.lang.IndexOutOfBoundsException: Index: 1, Size: 1 in Kotlin GSEA:java.lang.IndexOutOfBoundsException:索引:0,大小:0 - GSEA : java.lang.IndexOutOfBoundsException: Index: 0, Size: 0 java.lang.IndexOutOfBoundsException:无效的索引5,大小为5 - java.lang.IndexOutOfBoundsException: Invalid index 5, size is 5 例外:java.lang.IndexOutOfBoundsException:索引:0,大小:0 - Exception : java.lang.IndexOutOfBoundsException: Index: 0, Size: 0 java.lang.IndexOutOfBoundsException:索引:5,大小:5 - java.lang.IndexOutOfBoundsException: Index: 5, Size: 5 java.lang.IndexOutOfBoundsException:无效的索引0,大小为0 - java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0 异常 java.lang.IndexOutOfBoundsException:索引:1,大小:1 - Exception java.lang.IndexOutOfBoundsException: Index: 1, Size: 1 java.lang.IndexOutOfBoundsException:索引:3,大小:3 - java.lang.IndexOutOfBoundsException: Index: 3, Size: 3
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM