如何解决异常:在当前线程中找不到 OpenGL 上下文



我在 Java 中使用 libgdx 时遇到以下异常:

Exception in thread "Thread-12" java.lang.RuntimeException: No OpenGL context found in the current thread.
    at org.lwjgl.opengl.GLContext.getCapabilities(GLContext.java:124)
    at org.lwjgl.opengl.GL11.glGenTextures(GL11.java:1403)
    at com.badlogic.gdx.backends.lwjgl.LwjglGL20.glGenTexture(LwjglGL20.java:348)
    at com.badlogic.gdx.graphics.Texture.<init>(Texture.java:120)
    at com.badlogic.gdx.graphics.Texture.<init>(Texture.java:100)
    at com.badlogic.gdx.graphics.Texture.<init>(Texture.java:92)
    at com.badlogic.gdx.graphics.Texture.<init>(Texture.java:88)
    at Utils.Player$2.run(Player.java:79)
    at java.lang.Thread.run(Thread.java:748)

我该如何解决这个问题?我正在使用libgdx。玩家.java的第 79 行:

TextureRegion textureRegion = new TextureRegion(new Texture("textures/Textures.png"), 50, 15, 4, 4);

实际上,您不应该在libgdx中使用Thread,不建议使用Thread。Html不支持线程,在Html上不起作用。如果必须使用线程,

Gdx.app.postRunnable(new Runnable() {
         @Override
         public void run() {
            // process the result, e.g. add it to an Array<Result> field of the ApplicationListener.
            results.add(result);
         }
      });

要了解这里发生了什么,请查看官方文档https://github.com/libgdx/libgdx/wiki/Threading

在 Kotlin 中:

Gdx.app.postRunnable {
    // Your code in the context of OpenGL
}

最新更新