【发布时间】:2014-04-11 01:08:11
【问题描述】:
我第一次使用 LibGDX,在尝试在多路复用器中实现 InputAdapter 时遇到 NullPointerException。该消息似乎没有多大帮助,但我对我的 Java 相当生疏。我尝试使用实现 InputProcessor 接口的常规类,并且尝试记录任何会引发 Null 的内容,但我无法找到原因。
这是我的代码
import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.InputAdapter;
import com.badlogic.gdx.InputMultiplexer;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.PerspectiveCamera;
import com.badlogic.gdx.graphics.VertexAttributes.Usage;
import com.badlogic.gdx.graphics.g3d.Environment;
import com.badlogic.gdx.graphics.g3d.Material;
import com.badlogic.gdx.graphics.g3d.Model;
import com.badlogic.gdx.graphics.g3d.ModelBatch;
import com.badlogic.gdx.graphics.g3d.ModelInstance;
import com.badlogic.gdx.graphics.g3d.attributes.ColorAttribute;
import com.badlogic.gdx.graphics.g3d.environment.DirectionalLight;
import com.badlogic.gdx.graphics.g3d.utils.ModelBuilder;
public class Game implements ApplicationListener {
public PerspectiveCamera cam;
public Model model;
public ModelInstance instance;
public ModelBatch modelBatch;
public Environment environment;
public InputMultiplexer input;
public static int gWidth = 800;
public static int gHeight = 600;
@Override
public void create() {
this.modelBatch = new ModelBatch();
this.environment = new Environment();
this.environment.set(new ColorAttribute(ColorAttribute.AmbientLight, 0.4f, 0.4f, 0.4f, 1f));
this.environment.add(new DirectionalLight().set(0.8f, 0.8f, 0.8f, -1f, -0.8f, -0.2f));
/**
* Add input adapters.
*/
this.cam = new PerspectiveCamera(67, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
this.cam.position.set(10f, 20f, 0);
this.cam.lookAt(0, 0, 0);
this.cam.rotate(67f, 0f, 0f, 0f);
this.cam.near = 1f;
this.cam.far = 300f;
this.input = new InputMultiplexer(Gdx.input.getInputProcessor());
this.input.addProcessor(new MouseCamController());
Gdx.input.setInputProcessor(this.input);
this.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);
}
@Override
public void resize(int width, int height) {
}
@Override
public void render() {
Gdx.gl.glViewport(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
// if (this.cam.fieldOfView < 120) {
// this.cam.fieldOfView += 10;
// }
this.cam.update();
this.modelBatch.begin(this.cam);
this.modelBatch.render(instance, environment);
this.modelBatch.end();
}
@Override
public void pause() {
}
@Override
public void resume() {
}
@Override
public void dispose() {
this.modelBatch.dispose();
this.model.dispose();
}
public class MouseCamController extends InputAdapter {
@Override
public boolean scrolled(int amount) {
// Gdx.app.log("INPUT", "A: " + amount);
return true;
}
}
}
以下是异常详情:
Exception in thread "LWJGL Application" java.lang.NullPointerException
at com.badlogic.gdx.InputMultiplexer.mouseMoved(InputMultiplexer.java:107)
at com.badlogic.gdx.backends.lwjgl.LwjglInput.processEvents(LwjglInput.java:303)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:200)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:114)
【问题讨论】: