0
votes

I'm kind of new to LibGdx and I am not clear on this problem. I have a byte array of icons and I want to convert them into Textures.(byte[] -> Pixmap -> Texture) seems simple actually.

    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]);

Converting from byte[] to Pixmap works fine but the last and actuelly the easiest step causes this:

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)

It is probably a very simple issue and I don't know why I don't get it.

1
You can't generate Textures before LibGDX is initialized, and it must be done on LibGDX's main thread, which is the OpenGL thread.Tenfour04

1 Answers

0
votes

If you try to access OpenGL outside of the normal libgdx flow (e.g. in a separate thread) 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
   }
});

Another (recommended) way to load resources is by using the AssetManager.