我正在使用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)
当我尝试从我的
SignUpScreen
通过点击注册按钮到达LoginScreen
时,我会遇到相同的错误。然而,我不认为这是与button
有关的错误,因为如我之前所提到的,使用button
点击(如使用Login
button
从HomeScreen
到LoginScreen
)最初可以正常工作,因此我认为我的应用程序存在一个常见的error
。现在让我们关注于当我点击登出按钮后,在MainMenuScreen
到StartScreen
中发生的错误。我从在
MainMenuScreen
类顶部声明我的Game
变量开始: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);
}
我该如何解决这个问题?