Libgdx Pixmap to Texture导致NullPointerException

时间:2017-09-20 20:25:56

标签: java libgdx

我是LibGdx的新手,我不清楚这个问题。 我有一个字节数组的图标,我想将它们转换为纹理。(byte [] - > Pixmap - >纹理)实际上看起来很简单。

    byte[][] y = loadIcons();

    Pixmap[] icon = new Pixmap[100];
    for(int k = 0; k < 100; k++){
        if(y[k].length > 1000) {
            icon[k] = new Pixmap(y[k], 0, y[k].length);
        }
    }
    Texture iconTexture;

    iconTexture = new Texture(icon[0]);

从byte []转换为Pixmap工作正常,但最后一步也是最简单的步骤导致这一点:

java.lang.RuntimeException: Unable to start activity ComponentInfo{project## Heading ##}: java.lang.NullPointerException: Attempt to invoke interface method 'int com.badlogic.gdx.graphics.GL20.glGenTexture()' on a null object reference
                                                                     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
                                                                     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
                                                                     at android.app.ActivityThread.access$800(ActivityThread.java:144)
                                                                     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
                                                                     at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                     at android.os.Looper.loop(Looper.java:135)
                                                                     at android.app.ActivityThread.main(ActivityThread.java:5221)
                                                                     at java.lang.reflect.Method.invoke(Native Method)
                                                                     at java.lang.reflect.Method.invoke(Method.java:372)
                                                                     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
                                                                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
                                                                  Caused by: java.lang.NullPointerException: Attempt to invoke interface method 'int com.badlogic.gdx.graphics.GL20.glGenTexture()' on a null object reference
                                                                     at com.badlogic.gdx.graphics.Texture.<init>(Texture.java:120)
                                                                     at com.badlogic.gdx.graphics.Texture.<init>(Texture.java:104)
                                                                     at com.emis.htlauncher.AndroidLauncher.onCreate(AndroidLauncher.java:61)
                                                                     at android.app.Activity.performCreate(Activity.java:5937)
                                                                     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
                                                                     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
                                                                     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360) 
                                                                     at android.app.ActivityThread.access$800(ActivityThread.java:144) 
                                                                     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278) 
                                                                     at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                     at android.os.Looper.loop(Looper.java:135) 
                                                                     at android.app.ActivityThread.main(ActivityThread.java:5221) 
                                                                     at java.lang.reflect.Method.invoke(Native Method) 
                                                                     at java.lang.reflect.Method.invoke(Method.java:372)

这可能是一个非常简单的问题,我不知道为什么我没有得到它。

1 个答案:

答案 0 :(得分:0)

如果您尝试在正常的libgdx流程之外访问OpenGL(例如在单独的线程中)you have to use the main thread like this

Gdx.app.postRunnable(new Runnable() {
   @Override
   public void run() {
      // create icons here, com.badlogic.gdx.graphics.GL20 won't be null here
   }
});

另一种(推荐)加载资源的方法是using the AssetManager