簡體   English   中英

Libgdx Pixmap到Texture導致NullPointerException

[英]Libgdx Pixmap to Texture causes NullPointerException

我是LibGdx的新手,對此問題我不清楚。 我有一個字節的圖標數組,我想將它們轉換為Textures。(byte []-> Pixmap-> Texture)實際上看起來很簡單。

    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)

這可能是一個非常簡單的問題,我不知道為什么我不明白。

如果您嘗試在普通libgdx流之外訪問OpenGL(例如在單獨的線程中) ,則必須使用如下所示的主線程

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

加載資源的另一種(推薦)方法是使用AssetManager

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM