2017-09-20 17 views
0

私はLibGdxの新機能ですが、この問題については明確ではありません。 私はバイト配列のアイコンを持っており、それらをテクスチャに変換したいと思っています(バイト[] - > Pixmap - > Texture)ピックスマップにバイト[]からの変換Libgdx Pixmap to Textureが原因でNullPointerExceptionが発生する

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

が正常に動作しますが、最後とはactuelly最も簡単なステップは、これを原因:なぜドン

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) 

は、それはおそらく非常に単純な問題であり、私にはわかりませんそれを手に入れません。

+0

LibGDXが初期化される前にテクスチャを生成することはできません.LibGDXのメインスレッド(OpenGLスレッド)で行う必要があります。 – Tenfour04

答えて

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です。

関連する問題