batch.begin()LibgDDX中的空指针异常

时间:2012-09-16 14:42:36

标签: android nullpointerexception libgdx

我正在尝试将.png绘制到屏幕上,但我一直收到错误消息。我正在使用的类是Screen的子类。

这是我得到的错误:

FATAL EXCEPTION GLThread 1335 java.lang.NullPointerException  at SplashScreen.java.32

这一行是:

batch.begin()

这是我在Screen子类中的代码:

private SpriteBatch batch;
private Texture splashTexture;
private Camera camera;


 final int CAMERA_WIDTH = Gdx.graphics.getWidth();
static final int CAMERA_HEIGHT = Gdx.graphics.getHeight();



@Override
public void render(float delta) {
   Gdx.gl.glClearColor(0, 0, 0, 1);
   Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
   batch.begin();
   batch.draw(splashTexture, 0, 0);
   batch.end();

}

@Override
public void resize(int width, int height) {
   // TODO Auto-generated method stub

}

@Override
public void show() {
   splashTexture = new Texture(Gdx.files.internal("splash.png"));
   camera = new OrthographicCamera(CAMERA_WIDTH, CAMERA_HEIGHT);
    camera.position.set(CAMERA_WIDTH, CAMERA_HEIGHT, 0);
}

2 个答案:

答案 0 :(得分:1)

您的SpriteBatch为null,您可以创建纹理和相机但不创建批处理。你提到的断点与它无关。

private SpriteBatch batch;
private Texture splashTexture;
private Camera camera;

...

@Override
public void show(){
    splashTexture = new Texture(Gdx.files.internal("splash.png"));
    camera = new OrthographicCamera(CAMERA_WIDTH, CAMERA_HEIGHT);
    camera.position.set(CAMERA_WIDTH, CAMERA_HEIGHT, 0);

    batch = new SpriteBatch(); //create it like this.
}

...

答案 1 :(得分:0)

batch=new spritebatch();

,你的问题就解决了。