1

我不断收到NullPointerException第 39 行的消息,即 modelBatch.begin(cam);

我不知道它为什么这样做。如果你注意到原因,请告诉我。我整天都在为这个问题苦苦挣扎。我是android开发的新手,很容易犯愚蠢的错误。谢谢您的帮助。

public class Loading implements Screen {

private boolean AP;
private Chemistry chemistry;
public PerspectiveCamera cam;
public ModelBatch modelBatch;
public Model model;
public ModelInstance instance;
public Lights lights;

public Loading(boolean AP, Chemistry chemistry) {
    this.AP = AP;
    this.chemistry = chemistry;
}

@Override
public void render(float delta) {
    Gdx.gl.glViewport(0, 0, Gdx.graphics.getWidth(),
            Gdx.graphics.getHeight());
    Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);

    modelBatch.begin(cam);
    modelBatch.render(instance, lights);
    modelBatch.end();
}

@Override
public void resize(int width, int height) {
}

@Override
public void show() {
            modelBatch = new ModelBatch();
    lights = new Lights();
    lights.ambientLight.set(0.4f, 0.4f, 0.4f, 1f);
    lights.add(new DirectionalLight().set(0.8f, 0.8f, 0.8f, -1f, -0.8f,
            -0.2f));

    cam = new PerspectiveCamera(70, Gdx.graphics.getWidth(),
            Gdx.graphics.getHeight());
    cam.position.set(10f, 10f, 10f);
    cam.lookAt(0, 0, 0);
    cam.near = 0.1f;
    cam.far = 300f;
    cam.update();

    ModelBuilder modelBuilder = new ModelBuilder();
    model = modelBuilder.createBox(5f, 5f, 5f,
            new Material(ColorAttribute.createDiffuse(Color.GREEN)),
            Usage.Position | Usage.Normal);

    instance = new ModelInstance(model);

}
       ...
}
4

1 回答 1

2

将此移动到您的构造函数:

        modelBatch = new ModelBatch();
lights = new Lights();
lights.ambientLight.set(0.4f, 0.4f, 0.4f, 1f);
lights.add(new DirectionalLight().set(0.8f, 0.8f, 0.8f, -1f, -0.8f,
        -0.2f));

cam = new PerspectiveCamera(70, Gdx.graphics.getWidth(),
        Gdx.graphics.getHeight());
cam.position.set(10f, 10f, 10f);
cam.lookAt(0, 0, 0);
cam.near = 0.1f;
cam.far = 300f;
cam.update();

ModelBuilder modelBuilder = new ModelBuilder();
model = modelBuilder.createBox(5f, 5f, 5f,
        new Material(ColorAttribute.createDiffuse(Color.GREEN)),
        Usage.Position | Usage.Normal);

instance = new ModelInstance(model);

喜欢:

public Loading(boolean AP, Chemistry chemistry) {
    this.AP = AP;
    this.chemistry = chemistry;
    //here
}
于 2013-06-02T23:23:21.543 回答