libGDX Java(桌面):SelectBox抛出异常

时间:2015-05-26 00:10:58

标签: java libgdx scene2d

我在创建应用程序的选择框时遇到问题。我无法在其他任何地方找到解决方案 我的代码:

public class OptionsMenu implements Screen{
    private Stage stage = new Stage();

    private Skin skin = new Skin(Gdx.files.internal("jsonurlhere"), new TextureAtlas(Gdx.files.internal("atlasurlhere")));

    private String[] viewmodes = new String[] {"Fullscreen", "Windowed"};
    private SelectBox<String> viewmode;

    @Override
    public void show() {
        viewmode = new SelectBox<String>(skin);        
        viewmode.setItems(viewmodes);
        viewmode.setSelected("Fullscreen");

        stage.addActor(viewmode);
    }

    @Override
    public void render(float delta) {
        Gdx.gl.glClearColor(100, 100, 100, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
        stage.act();
        stage.draw();
    }
}

注意:这不是完整的代码,只是选择框的相关部分。

我的错误:

Exception in thread "LWJGL Application" java.lang.NullPointerException
    at com.badlogic.gdx.scenes.scene2d.ui.SelectBox.layout(SelectBox.java:193)
    at com.badlogic.gdx.scenes.scene2d.ui.Widget.validate(Widget.java:88)
    at com.badlogic.gdx.scenes.scene2d.ui.SelectBox.getPrefWidth(SelectBox.java:290)
    at com.badlogic.gdx.scenes.scene2d.ui.SelectBox.<init>(SelectBox.java:80)
    at com.badlogic.gdx.scenes.scene2d.ui.SelectBox.<init>(SelectBox.java:71)
    at com.lockedprogramming.pacificblitz.screens.OptionsMenu.show(OptionsMenu.java:49)
    at com.badlogic.gdx.Game.setScreen(Game.java:61)
    at com.lockedprogramming.pacificblitz.PacificBlitz.create(PacificBlitz.java:18)
    at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:143)
    at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:120)

Eclipse在运行时之前没有显示错误,如果我注释掉以下行,程序就不会抛出任何异常:

//viewmode = new SelectBox<String>(skin);
//viewmode.setItems(viewmodes);
//viewmode.setSelected("Fullscreen");
//stage.addActor(viewmode);

编辑:我的.json代码:

{
    com.badlogic.gdx.scenes.scene2d.ui.SelectBox$SelectBoxStyle: {
        default: {
            font: menufontblack, background: menubuttondefault,
            listStyle: { font: menufontblack, selection: menubuttonhover },
        },
    },
}

2 个答案:

答案 0 :(得分:2)

在皮肤json文件的SelectBoxStyle定义中有没有定义ScrollStyle 。我很确定你的问题就是问题所在。我可以通过删除ScrollStyle声明来重现我的一个libgdx项目中的问题。

尝试以下操作,只需为SelectBox设置默认的ScrollStyle:

com.badlogic.gdx.scenes.scene2d.ui.SelectBox$SelectBoxStyle: {
    default: {
        font: menufontblack, background: menubuttondefault,
        scrollStyle: default,
        listStyle: { font: menufontblack, selection: menubuttonhover },
    },
}

您还可以在此处查看示例uiskin json文件:https://github.com/libgdx/libgdx/blob/master/tests/gdx-tests-android/assets/data/uiskin.json

更新:您还需要在json中定义一个scrollstyle。类似的东西:

com.badlogic.gdx.scenes.scene2d.ui.ScrollPane$ScrollPaneStyle: {
    default: { vScroll: default-scroll, hScrollKnob: default-round-large, background: default-rect, hScroll: default-scroll, vScrollKnob: default-round-large }
},

最好的是检查github上的uiskin示例,看看这些纹理是如何形成的https://github.com/libgdx/libgdx/wiki/Skin#overview

答案 1 :(得分:0)

异常消息似乎表明SelectBox项的创建在类SelectBox的第193行失败;它无法设置布局,因此它返回null并且不被接受,即实例化SelectBox / Skin的方式不正确。