我第一次使用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)
答案 0 :(得分:1)
问题在于获得当前的输入处理器。由于没有,当它开始通过每个适配器时它实际上达到了NULL。愚蠢的问题,简单的答案。问题解决了,可供后人使用。
答案 1 :(得分:1)
当InputMultiplexer在其列表中到达null InputProcessor时,会出现问题。我尝试通过在返回的InputProcessor之后添加空处理器来复制它,并且不会发生错误。
只需从InputMultiplexer中删除任何null InputProcessor ,它应该可以正常工作。如果您不确定哪一个为null,请尝试列出所有处理器。
InputMultiplexer plexer;
// Add processors to plexer...
System.out.println(plexer.getProcessors().toString());