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

4 java libgdx

当我在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)
Run Code Online (Sandbox Code Playgroud)

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

TextureRegion textureRegion = new TextureRegion(new Texture("textures/Textures.png"), 50, 15, 4, 4);
Run Code Online (Sandbox Code Playgroud)

Abd*_*glu 6

实际上你不应该在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);
         }
      });
Run Code Online (Sandbox Code Playgroud)

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