相关疑难解决方法(0)

使用[]调用时出现奇怪的java.lang.ClassCastException

我正在为我的游戏使用LibDGX库.我遇到了常见的异常ClassCastException,但它发生在奇怪的情况下.

我正在使用LibGDX库中的Animation类.

只有在我使用操作时,我才会在此行上出错.[]

val tex = animation.keyFrames[0]
Run Code Online (Sandbox Code Playgroud)

如果我将其更改为get() 错误消失.

tex = animation.keyFrames.get(0)
Run Code Online (Sandbox Code Playgroud)

这是完整的方法.我简化了例子.

override fun create() {


    val frames = Array<TextureRegion>()
    frames.add(TextureRegion(Texture("badlogic.jpg")))

    val animation = Animation(frames)

    val tex1 = animation.keyFrames.get(0)  // Compiles

    val tex = animation.keyFrames[0]  // error - [Ljava.lang.Object; cannot be cast to [Lcom.badlogic.gdx.graphics.g2d.TextureRegion;


}
Run Code Online (Sandbox Code Playgroud)

奇怪,不是吗?

这是我用来最小化其他错误原因的Animation类.它与LibGDX api中的相同.

    public class Animation<T> {

    T[] keyFrames;
    private float frameDuration;


    public Animation (float frameDuration, Array<? extends T> keyFrames) {
        this.frameDuration = …
Run Code Online (Sandbox Code Playgroud)

java reflection libgdx kotlin

3
推荐指数
1
解决办法
399
查看次数

标签 统计

java ×1

kotlin ×1

libgdx ×1

reflection ×1