我在游戏中添加了一个新参数 (libGDX),但出现了一个奇怪的错误:
Exception in thread "main" java.lang.NullPointerException
at com.game.game.GameLaunching.<init>(GameLaunching.java:21)
at com.game.game.Main.main(Main.java:15)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)
当我尝试使用thm时,出现此错误。使用其他参数一切正常。 代码:
public class GameLaunching extends Game
{
public boolean isPause = false;
public boolean music, sound;
public String flag;
public String thm;
public int scoreForShop;
public boolean isFailture;
public GameLaunching(String flag, String thm, boolean music, boolean sound)
{
Gdx.app.info("1", thm); // error
this.flag = flag;
this.music = music;
this.sound = sound;
}
@Override
public void create()
{
isPause = false;
setScreen(new GameSurface(this));
}
主类代码:
public static void main(String[] args)
{
LwjglApplicationConfiguration cfg = new LwjglApplicationConfiguration();
cfg.title = "Game";
cfg.width = 800;
cfg.height = 480;
new LwjglApplication(new GameLaunching("8","night", false, true), cfg);
}
如何修复它?
最佳答案
您应该移动Gdx.app.info("1", thm);//错误
这行位于主方法中的 new LwjglApplication(new GameLaunching("8","night", false, true), cfg);
旁边
原因是在 Libgdx 中,库的初始化直到 create 方法开始之前才完成(该方法将由 Libgdx 基础结构调用以表明您的应用程序已启动),因此您无法访问 Gdx 。直到那时。
关于Java - 调用异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27674526/