【问题标题】:libGDX Java (Desktop): SelectBox throwing exceptionlibGDX Java(桌面):SelectBox 抛出异常
【发布时间】:2015-05-26 00:10:57
【问题描述】:

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

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 },
        },
    },
}

【问题讨论】:

  • 请同时显示你皮肤的 json 文件。 SelectBox 样式似乎有问题。
  • 刚刚添加了@donfuxx

标签: java libgdx scene2d


【解决方案1】:

在皮肤 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 中定义一个滚动样式。类似:

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

【讨论】:

  • 谢谢,但这给了我No com.badlogic.gdx.scenes.scene2d.ui.ScrollPane$ScrollPaneStyle registered with name: default 我是否需要创建一个 ScrollPane$ScrollPaneStyle 部分以及需要哪些资产(纹理等)
【解决方案2】:

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

【讨论】:

  • 谢谢,但是我该如何解决呢?您需要其他部分的代码还是 .json 文件?
  • 发布SelectBox.java的内容
  • 那是原来的libGDX类,你可以在网上找到它,这可能无论如何也无济于事
猜你喜欢
  • 2012-07-23
  • 2012-05-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-05-24
  • 2018-09-18
  • 2011-02-07
  • 2016-08-10
相关资源
最近更新 更多