Java stage.act()上的NullPointerException

Java stage.act()上的NullPointerException,java,libgdx,Java,Libgdx,当我运行游戏并单击屏幕退出启动屏幕时,它会将我带到主菜单,但它只是冻结。我找到了错误告诉我的那行代码,但没有任何帮助。(MainMenu.java:124)那行代码只是显示了stage.act();而不是舞台;如果我试图删除或注释掉stage.act();。(MyGdxGame.java:31)然后指向我的super.render();。我不知道怎么了,任何帮助都会很好 错误: Exception in thread "LWJGL Application" java.lang.NullPoint

当我运行游戏并单击屏幕退出启动屏幕时,它会将我带到主菜单,但它只是冻结。我找到了错误告诉我的那行代码,但没有任何帮助。(MainMenu.java:124)那行代码只是显示了stage.act();而不是舞台;如果我试图删除或注释掉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)
MyGDX游戏代码:

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();
    }
}
主菜单代码:

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() {
    }

}

我假设您从未调用过主菜单屏幕的create方法。所以阶段未初始化并引发NullPointer异常


使用show method初始化屏幕变量。

初始化皮肤时可能出现问题,请尝试将其从演员身上移除并告诉我。splashscreen代码???@Mehdi这是什么意思?当我试图去除皮肤时;从我的代码中我得到了更多的错误。对不起,你这是什么意思?在哪里添加Show方法?当screen成为游戏的当前屏幕时调用Show()方法。具有show()方法的屏幕界面。您已重写此方法,但该方法为空。因此,将create()方法的代码放在show()方法中。