我有一个启动类,应该只显示一个启动图像,但它会导致空指针异常。它激动了,因为我已经在其他方面使用了这个方法。我已经阅读了大量文档,看看我哪里出错,但我似乎无法找到它。
Splash.java class
package com.me.fypapplication;
import com.badlogic.gdx.Game;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.Texture.TextureFilter;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
public class Splash implements Screen{
private SpriteBatch spriteBatch;
private Texture splashTexture;
private Sprite sprite;
private Game myGame;
private OrthographicCamera camera;
public Splash (Game g)
{
myGame = g;
}
public void create() {
camera = new OrthographicCamera(800,400);
splashTexture = new Texture(Gdx.files.internal("data/splashtest.gif"));
sprite = new Sprite(splashTexture);
sprite.setSize(512, 512);
sprite.setPosition(256, 256);
spriteBatch = new SpriteBatch();
splashTexture.setFilter(TextureFilter.Linear, TextureFilter.Linear);
}
@Override
public void render(float delta) {
Gdx.gl.glClearColor(0.1f, 0.1f, 0.1f, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
camera.update();
spriteBatch.begin();
sprite.draw(spriteBatch);
spriteBatch.end();
}
如果有人能提供帮助,将不胜感激!
EDIT! 忘记了错误
Exception in thread "LWJGL Application" java.lang.NullPointerException
at com.me.fypapplication.Splash.render(Splash.java:51)
at com.badlogic.gdx.Game.render(Game.java:46)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:190)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:110)
答案 0 :(得分:3)
您的相机为空,因为没有任何内容正在调用 create()。如果你正在实现一个libgdx屏幕并且需要初始化,那么在调用 render()之前你应该覆盖 show()方法。