LWJGL应用"显示java.lang.NullPointerException

时间:2016-10-03 19:11:28

标签: java android android-studio null libgdx

我的LibGdx应用程序(Java)中出现空指针错误。我研究了所有代码而没有研究,主要问题是 java.lang.NullPointerException 出现在多次调用batch.draw()stage.draw()方法之前< em>(执行错误)不是立即。

这是错误:

: here2
: here3
: here2
: here3
: here2
: here3
: here2
: here3
: here2
: here3
Exception in thread "LWJGL Application" java.lang.NullPointerException
    at com.badlogic.gdx.graphics.g2d.SpriteBatch.draw(SpriteBatch.java:598)
    at com.platanobit.actors.Delivery.draw(Delivery.java:60)
    at com.badlogic.gdx.scenes.scene2d.Group.drawChildren(Group.java:110)
    at com.badlogic.gdx.scenes.scene2d.Group.draw(Group.java:57)
    at com.badlogic.gdx.scenes.scene2d.Stage.draw(Stage.java:128)
    at com.platanobit.screens.GameScreen.render(GameScreen.java:24)
    at com.badlogic.gdx.Game.render(Game.java:46)
    at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:223)
    at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:124)

这里是第60行的代码(com.platanobit.actors.Delivery.draw(Delivery.java:60):

@Override
    public void draw(Batch batch, float parentAlpha) {
        super.draw(batch, parentAlpha);

        if (dodging) {
            batch.draw(dodgingTexture, screenRectangle.x, screenRectangle.y + screenRectangle.height / 4, screenRectangle.width,
                    screenRectangle.height * 3 / 4);
        } else if (hit) {
            // When he's hit we also want to apply rotation if the body has been rotated
            batch.draw(hitTexture, screenRectangle.x, screenRectangle.y, screenRectangle.width * 0.5f,
                    screenRectangle.height * 0.5f, screenRectangle.width, screenRectangle.height, 1f, 1f,
                    (float) Math.toDegrees(body.getAngle()));
        } else if (jumping) {
            batch.draw(jumpingTexture, screenRectangle.x, screenRectangle.y, screenRectangle.width,
                    screenRectangle.height);
        } else {
            // Running
            stateTime += Gdx.graphics.getDeltaTime();
            if(batch != null){
            batch.draw(runningAnimation.getKeyFrame(stateTime, true), screenRectangle.x, screenRectangle.y,
                    screenRectangle.getWidth(), screenRectangle.getHeight());
            } else {
                Gdx.app.log("", "here");
            }
        }
        Gdx.app.log("","here2");
    }

以下是第24行(GameScreen.java)的代码:

@Override
    public void render(float delta) {
        // Clear the screen
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
        // Update the stage
        stage.draw();
        Gdx.app.log("","here3");
        stage.act(delta);
    }

注意:请查看Gdx.app.log("","Here");代码以进行调试。如果有人需要别的东西,请帮助我:/

2 个答案:

答案 0 :(得分:0)

仔细检查以确保在传递给此方法之前初始化SpriteBatch,并确保已调用SpriteBatch.begin();如果这不是问题,请准确说明Delivery.java:60

的行

答案 1 :(得分:0)

在libgdx中,在屏幕上绘制任何内容之前,您应该启动spriteBatch,在绘制之后,您应该结束Sprite批处理。批处理用于绘制引用纹理(区域)的2D矩形。该类将批处理绘图命令并对其进行优化以供GPU处理。 要使用Batch绘制内容,必须首先调用begin()方法,该方法将设置适当的渲染状态。完成绘图后,你必须调用end()来实际绘制你指定的东西。

 public void render(SpriteBatch batch){
 batch.begin();
 stage.draw();
 sprite.draw(batch);
 batch.end();