我正在使用 LibGDX 为 Android 开发游戏。我已经创建了几个屏幕,如果我似乎还没有访问过它们,我可以成功地访问它们。例如,我可以从StartScreen
到HomeScreen
到LoginScreen
到MainMenuScreen
就好了。但是,当我尝试通过单击注销按钮从我MainMenuScreen
的应用程序切换到我StartScreen
的应用程序时,我得到一个 NPE:
Exception in thread "LWJGL Application" java.lang.NullPointerException
at com.mathsvszombies2.game.Screens.MainMenuScreen$3.clicked(MainMenuScreen.java:145)
at com.badlogic.gdx.scenes.scene2d.utils.ClickListener.touchUp(ClickListener.java:89)
at com.badlogic.gdx.scenes.scene2d.InputListener.handle(InputListener.java:58)
at com.badlogic.gdx.scenes.scene2d.Stage.touchUp(Stage.java:353)
at com.badlogic.gdx.backends.lwjgl.LwjglInput.processEvents(LwjglInput.java:332)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:217)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:126)
当我在单击 Sign Up 后尝试从我转到时,我得到了同样的错误SignUpScreen
,但是,我不认为这是一个错误,因为正如我之前提到的,使用点击(例如至于使用)。因此,我相信我的应用程序中有一个共同点。让我们关注我的点击错误。LoginScreen
button
button
screens
button
HomeScreen
LoginScreen
Login
button
error
MainMenuScreen
StartScreen
logout button
我首先在类Game
的顶部声明我的变量MainMenuScreen
:
private Game game;
这是我的 MainMenuScreen 构造函数:
public MainMenuScreen(Game game) {
app.log("Screen", "Main Menu Screen");
camera= new OrthographicCamera();
viewport = new FitViewport(MathsVsZombies.V_WIDTH, MathsVsZombies.V_HEIGHT, camera);
stage = new Stage(viewport);
}
这是我尝试切换屏幕的地方:
logoutBtn.addListener(new ClickListener(){
@Override
public void clicked(InputEvent event, float x, float y) {
LoginScreen.isLoggedIn = false;
game.setScreen(new StartScreen(game));
}
});
这是我的 StartScreen 类 - 我首先在 StartScreen 类的顶部声明我的 Game 变量:
private Game game;
这是我的 StartScreen 构造函数:
public StartScreen(Game game){
Gdx.app.log("Screen", "Start Screen");
this.game = game;
camera= new OrthographicCamera();
viewport = new FitViewport(MathsVsZombies.V_WIDTH, MathsVsZombies.V_HEIGHT, camera);
stage = new Stage(viewport);
StartScreenActor actor = new StartScreenActor();
stage.addActor(actor);
Gdx.input.setInputProcessor(stage);
}
我怎样才能解决这个问题?