0

I created a project and used animation. In my opinion, the code is normal but it fails when running the app.

Image:

[File images]

Code:

    Texture texture2;
    SpriteBatch batch;
    float e=0;
    Animation ani;
    TextureRegion[] tr;
    TextureRegion[][] tr2;
    int index,i,j;

    @Override
    public void create()
    {
        try{
        texture2 = new Texture(Gdx.files.internal("logo.png"));}
        catch(Exception ex){}
        batch = new SpriteBatch();
        tr=new TextureRegion[2];
        tr2=TextureRegion.split(texture2,94,18);
        ani=new Animation(1f/4f,tr);
        index=0;
        for(i=0;i<2;i++){
            for(j=0;j<2;j++){
                tr[index++]=tr2[j][i];
            }
        }
    }

    @Override
    public void render()
    {        
        Gdx.gl.glClearColor(1, 1, 1, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
        e+=Gdx.graphics.getDeltaTime();
        batch.begin();
        batch.draw(ani.getKeyFrame(e),0,0);
        /*batch.draw(texture, Gdx.graphics.getWidth() / 4, 0, 
                   Gdx.graphics.getWidth() / 2, Gdx.graphics.getWidth() / 2);*/
        batch.end();
    }

Logs:

    FATAL EXCEPTION: GLThread 2510
    Process: com.mycompany.mygame4, PID: 21018
    java.lang.IllegalArgumentException: texture cannot be null.
        at com.badlogic.gdx.graphics.g2d.TextureRegion.<init>(TextureRegion.java:37)
        at com.badlogic.gdx.graphics.g2d.TextureRegion.split(TextureRegion.java:276)
        at com.mycompany.mygame4.MyGdxGame.create(MyGdxGame.java:25)
        at com.badlogic.gdx.backends.android.AndroidGraphics.onSurfaceChanged(AndroidGraphics.java:236)
        at android.opengl.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:1555)
        at android.opengl.GLSurfaceView$GLThread.run(GLSurfaceView.java:1270)

Please help me to figure out what's going wrong.

Hafez Divandari
  • 8,381
  • 4
  • 46
  • 63
  • what line is TextureRegion:37 ? – vmrvictor Jan 16 '20 at 06:04
  • @vmrvictor I do not understand. at line 37 the code is very normal.Before I try and catch the texture, it says 'cannot load file logo.png' – hoichemgio9x Jan 16 '20 at 06:06
  • It could be but read the exception: com.badlogic.gdx.graphics.g2d.TextureRegion.(TextureRegion.java:37), there you are passing null as a parameter, you can always debug on that line and see where It comes from – vmrvictor Jan 16 '20 at 06:08
  • @vmrvictor ok. I tries and next error:FATAL EXCEPTION: GLThread 2638 Process: com.mycompany.mygame4, PID: 32575 java.lang.NullPointerException: Attempt to read from null array at com.mycompany.mygame4.MyGdxGame.create(MyGdxGame.java:30) at com.badlogic.gdx.backends.android.AndroidGraphics.onSurfaceChanged(AndroidGraphics.java:236) at android.opengl.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:1555) at android.opengl.GLSurfaceView$GLThread.run(GLSurfaceView.java:1270) – hoichemgio9x Jan 16 '20 at 06:18
  • @vmrvictor I fixed it. The reason is that I have set the wrong frame number. Thanks for the support – hoichemgio9x Jan 16 '20 at 06:42

0 Answers0