stage.act()上的NullPointerException(NullPointerException on stage.act())
当我运行游戏并点击屏幕退出我的启动画面时,它应该将我带到我的MainMenu,但它只是冻结。 我找到了错误告诉我的代码行,但没有帮助。(MainMenu.java:124)该行只显示了stage.act(); 比stage.draw(); 如果我试图删除或注释掉stage.act();.(MyGdxGame.java:31)然后指向我的super.render();. 我不知道什么是错的,任何帮助都会很棒。
错误:
Exception in thread "LWJGL Application" java.lang.NullPointerException at com.jack.mygdxgame.screens.MainMenu.render(MainMenu.java:124) at com.badlogic.gdx.Game.render(Game.java:46) at com.jack.mygdxgame.MyGdxGame.render(MyGdxGame.java:31) at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:225) at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:126)
MyGdxGame代码:
package com.jack.mygdxgame; import com.badlogic.gdx.Game; import com.badlogic.gdx.Gdx; import com.badlogic.gdx.Screen; import com.badlogic.gdx.graphics.Texture; import com.badlogic.gdx.graphics.g2d.SpriteBatch; import com.jack.mygdxgame.screens.GameScreen; import com.jack.mygdxgame.splash.SplashScreen; public class MyGdxGame extends Game { public static int nWidth; public static int nHeight; private Game game; public MyGdxGame() { game = this; } @Override public void create() { nWidth = Gdx.graphics.getWidth(); nHeight = Gdx.graphics.getHeight(); setScreen(new SplashScreen(this)); } @Override public void render() { super.render(); } }
MainMenu代码:
package com.jack.mygdxgame.screens; import com.badlogic.gdx.Game; import com.badlogic.gdx.Gdx; import com.badlogic.gdx.Screen; import com.badlogic.gdx.graphics.GL20; import com.jack.mygdxgame.MyGdxGame; import com.badlogic.gdx.ApplicationAdapter; import com.badlogic.gdx.Gdx; import com.badlogic.gdx.graphics.Color; import com.badlogic.gdx.graphics.GL20; import com.badlogic.gdx.graphics.Pixmap; import static com.badlogic.gdx.graphics.Pixmap.Format.RGB888; import com.badlogic.gdx.graphics.Texture; import com.badlogic.gdx.graphics.g2d.BitmapFont; import com.badlogic.gdx.scenes.scene2d.InputEvent; import com.badlogic.gdx.scenes.scene2d.Stage; import com.badlogic.gdx.scenes.scene2d.ui.TextButton; import com.badlogic.gdx.scenes.scene2d.ui.TextButton.TextButtonStyle; import com.badlogic.gdx.scenes.scene2d.utils.ClickListener; import com.badlogic.gdx.scenes.scene2d.ui.Skin; public class MainMenu implements Screen { Stage stage; Skin skin; private Game game; public MainMenu(Game game) { this.game = game; } public void create() { // load assets int buttonOffSet = 20; stage = new Stage(); Gdx.input.setInputProcessor(stage);// Make the stage consume events createBasicSkin(); TextButton newGameButton = new TextButton("Start game", (com.badlogic.gdx.scenes.scene2d.ui.Skin) skin); // Use the initialized skin newGameButton.setPosition(Gdx.graphics.getWidth() / 2 - Gdx.graphics.getWidth() / 8, Gdx.graphics.getHeight() / 2 + (newGameButton.getHeight() + buttonOffSet)); newGameButton.addListener(new ClickListener() { @Override public void clicked(InputEvent event, float x, float y) { System.out.println("Start game button clicked"); } }); stage.addActor(newGameButton); TextButton loadButton = new TextButton("Load game", (com.badlogic.gdx.scenes.scene2d.ui.Skin) skin); // Use the initialized skin loadButton.setPosition(Gdx.graphics.getWidth() / 2 - Gdx.graphics.getWidth() / 8, Gdx.graphics.getHeight() / 2); loadButton.addListener(new ClickListener() { @Override public void clicked(InputEvent event, float x, float y) { System.out.println("Load Button clicked"); } }); stage.addActor(loadButton); TextButton settingsButton = new TextButton("Settings", (com.badlogic.gdx.scenes.scene2d.ui.Skin) skin); settingsButton.setPosition(Gdx.graphics.getWidth() / 2 - Gdx.graphics.getWidth() / 8, Gdx.graphics.getHeight() / 2 - (settingsButton.getHeight() + buttonOffSet)); settingsButton.addListener(new ClickListener() { @Override public void clicked(InputEvent event, float x, float y) { System.out.println("Settinga Button clicked"); } }); stage.addActor(settingsButton); TextButton exitGameButton = new TextButton("Exit game", (com.badlogic.gdx.scenes.scene2d.ui.Skin) skin); exitGameButton.setPosition(Gdx.graphics.getWidth() / 2 - Gdx.graphics.getWidth() / 8, Gdx.graphics.getHeight() / 2 - 2 * (newGameButton.getHeight() + buttonOffSet)); exitGameButton.addListener(new ClickListener() { @Override public void clicked(InputEvent event, float x, float y) { System.out.println("Exit game button clicked"); System.exit(0); } }); stage.addActor(exitGameButton); } private void createBasicSkin() { //Create a font BitmapFont font = new BitmapFont(); skin = new Skin(); skin.add("default", font); //Create a texture Pixmap pixmap = new Pixmap((int) Gdx.graphics.getWidth() / 4, (int) Gdx.graphics.getHeight() / 10, Pixmap.Format.RGB888); pixmap.setColor(Color.WHITE); pixmap.fill(); skin.add("background", new Texture(pixmap)); //Create a button style TextButton.TextButtonStyle textButtonStyle = new TextButton.TextButtonStyle(); textButtonStyle.up = skin.newDrawable("background", Color.GRAY); textButtonStyle.down = skin.newDrawable("background", Color.DARK_GRAY); textButtonStyle.checked = skin.newDrawable("background", Color.DARK_GRAY); textButtonStyle.over = skin.newDrawable("background", Color.LIGHT_GRAY); textButtonStyle.font = skin.getFont("default"); skin.add("default", textButtonStyle); } @Override public void show() { } @Override public void render(float f) { GL20 gl = Gdx.graphics.getGL20(); gl.glClear(GL20.GL_COLOR_BUFFER_BIT); Gdx.gl.glClearColor(1, 1, 1, 1); stage.act(); stage.draw(); } @Override public void resize(int i, int i1) { } @Override public void pause() { } @Override public void resume() { } @Override public void hide() { } @Override public void dispose() { } }
When I run my game and click the screen to exit my splash screen it should bring me to my MainMenu but instead it just freezes. I found the line of code that the error's were telling me but it was no help.(MainMenu.java:124) that line just showed stage.act(); than stage.draw(); if I tried to remove or comment out stage.act();.(MyGdxGame.java:31) then pointed at my super.render();. I have no idea what's wrong, any help at all would be great.
Errors:
Exception in thread "LWJGL Application" java.lang.NullPointerException at com.jack.mygdxgame.screens.MainMenu.render(MainMenu.java:124) at com.badlogic.gdx.Game.render(Game.java:46) at com.jack.mygdxgame.MyGdxGame.render(MyGdxGame.java:31) at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:225) at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:126)
MyGdxGame Code:
package com.jack.mygdxgame; import com.badlogic.gdx.Game; import com.badlogic.gdx.Gdx; import com.badlogic.gdx.Screen; import com.badlogic.gdx.graphics.Texture; import com.badlogic.gdx.graphics.g2d.SpriteBatch; import com.jack.mygdxgame.screens.GameScreen; import com.jack.mygdxgame.splash.SplashScreen; public class MyGdxGame extends Game { public static int nWidth; public static int nHeight; private Game game; public MyGdxGame() { game = this; } @Override public void create() { nWidth = Gdx.graphics.getWidth(); nHeight = Gdx.graphics.getHeight(); setScreen(new SplashScreen(this)); } @Override public void render() { super.render(); } }
MainMenu code:
package com.jack.mygdxgame.screens; import com.badlogic.gdx.Game; import com.badlogic.gdx.Gdx; import com.badlogic.gdx.Screen; import com.badlogic.gdx.graphics.GL20; import com.jack.mygdxgame.MyGdxGame; import com.badlogic.gdx.ApplicationAdapter; import com.badlogic.gdx.Gdx; import com.badlogic.gdx.graphics.Color; import com.badlogic.gdx.graphics.GL20; import com.badlogic.gdx.graphics.Pixmap; import static com.badlogic.gdx.graphics.Pixmap.Format.RGB888; import com.badlogic.gdx.graphics.Texture; import com.badlogic.gdx.graphics.g2d.BitmapFont; import com.badlogic.gdx.scenes.scene2d.InputEvent; import com.badlogic.gdx.scenes.scene2d.Stage; import com.badlogic.gdx.scenes.scene2d.ui.TextButton; import com.badlogic.gdx.scenes.scene2d.ui.TextButton.TextButtonStyle; import com.badlogic.gdx.scenes.scene2d.utils.ClickListener; import com.badlogic.gdx.scenes.scene2d.ui.Skin; public class MainMenu implements Screen { Stage stage; Skin skin; private Game game; public MainMenu(Game game) { this.game = game; } public void create() { // load assets int buttonOffSet = 20; stage = new Stage(); Gdx.input.setInputProcessor(stage);// Make the stage consume events createBasicSkin(); TextButton newGameButton = new TextButton("Start game", (com.badlogic.gdx.scenes.scene2d.ui.Skin) skin); // Use the initialized skin newGameButton.setPosition(Gdx.graphics.getWidth() / 2 - Gdx.graphics.getWidth() / 8, Gdx.graphics.getHeight() / 2 + (newGameButton.getHeight() + buttonOffSet)); newGameButton.addListener(new ClickListener() { @Override public void clicked(InputEvent event, float x, float y) { System.out.println("Start game button clicked"); } }); stage.addActor(newGameButton); TextButton loadButton = new TextButton("Load game", (com.badlogic.gdx.scenes.scene2d.ui.Skin) skin); // Use the initialized skin loadButton.setPosition(Gdx.graphics.getWidth() / 2 - Gdx.graphics.getWidth() / 8, Gdx.graphics.getHeight() / 2); loadButton.addListener(new ClickListener() { @Override public void clicked(InputEvent event, float x, float y) { System.out.println("Load Button clicked"); } }); stage.addActor(loadButton); TextButton settingsButton = new TextButton("Settings", (com.badlogic.gdx.scenes.scene2d.ui.Skin) skin); settingsButton.setPosition(Gdx.graphics.getWidth() / 2 - Gdx.graphics.getWidth() / 8, Gdx.graphics.getHeight() / 2 - (settingsButton.getHeight() + buttonOffSet)); settingsButton.addListener(new ClickListener() { @Override public void clicked(InputEvent event, float x, float y) { System.out.println("Settinga Button clicked"); } }); stage.addActor(settingsButton); TextButton exitGameButton = new TextButton("Exit game", (com.badlogic.gdx.scenes.scene2d.ui.Skin) skin); exitGameButton.setPosition(Gdx.graphics.getWidth() / 2 - Gdx.graphics.getWidth() / 8, Gdx.graphics.getHeight() / 2 - 2 * (newGameButton.getHeight() + buttonOffSet)); exitGameButton.addListener(new ClickListener() { @Override public void clicked(InputEvent event, float x, float y) { System.out.println("Exit game button clicked"); System.exit(0); } }); stage.addActor(exitGameButton); } private void createBasicSkin() { //Create a font BitmapFont font = new BitmapFont(); skin = new Skin(); skin.add("default", font); //Create a texture Pixmap pixmap = new Pixmap((int) Gdx.graphics.getWidth() / 4, (int) Gdx.graphics.getHeight() / 10, Pixmap.Format.RGB888); pixmap.setColor(Color.WHITE); pixmap.fill(); skin.add("background", new Texture(pixmap)); //Create a button style TextButton.TextButtonStyle textButtonStyle = new TextButton.TextButtonStyle(); textButtonStyle.up = skin.newDrawable("background", Color.GRAY); textButtonStyle.down = skin.newDrawable("background", Color.DARK_GRAY); textButtonStyle.checked = skin.newDrawable("background", Color.DARK_GRAY); textButtonStyle.over = skin.newDrawable("background", Color.LIGHT_GRAY); textButtonStyle.font = skin.getFont("default"); skin.add("default", textButtonStyle); } @Override public void show() { } @Override public void render(float f) { GL20 gl = Gdx.graphics.getGL20(); gl.glClear(GL20.GL_COLOR_BUFFER_BIT); Gdx.gl.glClearColor(1, 1, 1, 1); stage.act(); stage.draw(); } @Override public void resize(int i, int i1) { } @Override public void pause() { } @Override public void resume() { } @Override public void hide() { } @Override public void dispose() { } }
原文:
最满意答案
通常当我收到该错误时(警告:轶事),这是因为我在迭代器之后错过了
do
关键字,或者是在视图中占用块的haml函数。我没有看到
link_to
行有什么问题 - 你可以发布包含错误的文件的来源(并指出哪一行是第23行)?It was a configuration issue. Created a new project, copied my files. Now its working.
相关问答
更多-
我相信你是从Windows操作系统这样做的。 您必须在JSON参数周围使用双引号而不是单引号。 而你必须使用反斜杠(\)来避开内部的双引号。 -d "{\"username\":\"xyz\",\"password\":\"xyz\"}" 希望这会帮助你。 I believe you are doing this from Windows operating system. You have to use double quotes instead of single around the JSON pa ...
-
在IntelliJ中运行RoboElectric 2测试的仪表运行错误(Instrumentation error running RoboElectric 2 tests in IntelliJ)[2022-03-14]
“无法找到工具”建议您尝试将其作为Android测试运行 - 是这种情况吗? 它应该作为JUnit测试运行(使用RoboElectric表明你已经编写了标准的单元测试) "Unable to find instrumentation" suggests you try to run this as Android test - is it the case? It should be run as JUnit test (usage of RoboElectric suggests you've writt ... -
REPL(read-evaluate-print-loop)不能在下一行看到,看循环是否以普通解析器可以用的方式结束。 换句话说,当python的普通解析器解析你的文件时,它会看到缩进器并知道循环在sum+=num之后结束。 当REPL到达同一行时,它无法知道是否应该有另一个语句,因此它假设有另一个语句。 告诉REPL这是循环的终端线(或者更一般地说,任何一组命令的结束)的方式是输入一个空行。 The REPL (read-evaluate-print-loop) can't look ahead at t ...
-
作为答案: 你忘了在ProcessBuilder提到你的实际Python脚本 As an answer: You forgot to mention your actual python script in the ProcessBuilder
-
我得到了它的工作,我认为安装过程是我出错的地方。 我在全球和本地安装了一切。 所以这次我在本地和全局卸载了所有dalekjs包,然后按照这个过程进行操作 sudo npm install dalek-cli -g sudo npm install dalekjs 它对我来说很好 I got it working, I think it was the installation process is where I went wrong. I installed everything globally a ...
-
试试这个FileExchange提交: 在Vim中突出显示Matlab文件 在Vim中编辑Matlab文件 Try this FileExchange submissions: Highlighting Matlab files in Vim Editing Matlab files in Vim
-
在处理JSX语法之前,您需要设置Babel。 有两个选项:将配置添加到package.json ,或创建一个名为.babelrc的文件,其中包含配置。 首先,安装babel-preset-react : $ npm i babel-preset-react --save 接下来,将以下内容添加到package.json : "babel": { "presets": [ "react" ] } (或者将那个没有“babel”键的对象添加到.babelrc ) 您可能想要使用的另一个预设是babel- ...
-
当对象具有重复的属性名称时,PhantomJS会抛出语法错误,例如: var object = { foo: 1, foo: 2 }; 大多数浏览器不会将此视为语法错误,只使用最后一个属性定义。 PhantomJS throws a syntax error when an object has duplicate property names, eg: var object = { foo: 1, foo: 2 }; Most browsers don't view this as a syntax ...
-
从终端运行带有OpenCV的java时发生JNI错误(JNI error has occurred when running java with OpenCV from terminal)[2023-10-23]
-cp是-classpath缩写。 所以我怀疑当你同时使用它们时,第二个会覆盖第一个。 您可以在类路径上分隔多个条目:如文档中所述: 设置类路径 。 (实际上,它看起来像是文档中的拼写错误,它应该是;作为分隔符。你可以尝试两者) 如果你从目录home/pi/Desktop/MultiSensor_v2.0_Java/执行你的命令: java -Djava.library.path="opencv-3.2.0-java/build/lib" -cp .:opencv-3.2.0-java/build/bin/ ... -
通常当我收到该错误时(警告:轶事),这是因为我在迭代器之后错过了do关键字,或者是在视图中占用块的haml函数。 我没有看到link_to行有什么问题 - 你可以发布包含错误的文件的来源(并指出哪一行是第23行)? It was a configuration issue. Created a new project, copied my files. Now its working.