You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

LibGDX新手创建List控件时报空指针异常的问题求助

问题原因
  • 你的SimpleSkin.json中ListStyle配置缺少必填字段:LibGDX的List控件要求ListStyle必须指定selection(选中状态的Drawable),缺少该配置会触发空指针,对应你堆栈里List.layout方法的报错。
  • 控件初始化逻辑位置错误:你把List的创建、添加到Stage的逻辑写在了render方法中,该方法每帧都会执行,会导致重复创建大量控件,最终引发内存溢出和逻辑异常。
  • Stage对象未初始化:你声明了stage变量但没有实例化,即使解决List创建问题,后续调用stage.act()和stage.draw()也会触发空指针。
解决方案

1. 修正Skin配置

在你的SimpleSkin.json中补全ListStyle的必填配置,确保你引用的selection、listBackground对应你的uiskin.atlas中存在的资源:

{
    "com.badlogic.gdx.graphics.Color": {
      "black": { "r": 0, "g": 0, "b": 0, "a": 1 },
      "white": { "r": 1, "g": 1, "b": 1, "a": 1 },
      "gray": { "r": 0.5, "g": 0.5, "b": 0.5, "a": 1 }
    },

  "com.badlogic.gdx.graphics.g2d.BitmapFont": {
      "default-font": { "file": "font.fnt" }
    },

  "com.badlogic.gdx.scenes.scene2d.ui.Label$LabelStyle": {
    "default": {
        "font": "default-font",
        "fontColor": "black"
      }
    },
  

  "com.badlogic.gdx.scenes.scene2d.ui.List$ListStyle": {
    "default": {
      "font": "default-font",
      "selection": "selection",
      "background": "listBackground",
      "fontColorSelected": "white",
      "fontColorUnselected": "black"
    }
  }
}

如果你的atlas中暂时没有对应的drawable资源,可以在初始化Skin后临时创建测试用资源:

// 初始化skin后添加以下代码
Pixmap tempPixmap = new Pixmap(1,1, Pixmap.Format.RGBA8888);
tempPixmap.setColor(Color.GRAY);
tempPixmap.fill();
skin.add("selection", new Texture(tempPixmap));
tempPixmap.setColor(Color.WHITE);
tempPixmap.fill();
skin.add("listBackground", new Texture(tempPixmap));
tempPixmap.dispose();

2. 修正代码逻辑

将控件初始化逻辑移到构造方法中,拆分更新和渲染逻辑:

public class ChangeSongState extends State {

    ScrollPane scrollPane;
    List<String> list;
    Skin skin;
    TextureAtlas atlas;
    float gameWidth, gameHeight;
    private Stage stage;

    public ChangeSongState(GameStateManager game) {
        super(game);
        atlas = new TextureAtlas(Gdx.files.internal("uiskin.atlas"));
        skin = new Skin(Gdx.files.internal("SimpleSkin.json"), atlas);
        
        // 临时资源测试用,atlas有对应资源可以删除
        Pixmap tempPixmap = new Pixmap(1,1, Pixmap.Format.RGBA8888);
        tempPixmap.setColor(Color.GRAY);
        tempPixmap.fill();
        skin.add("selection", new Texture(tempPixmap));
        tempPixmap.setColor(Color.WHITE);
        tempPixmap.fill();
        skin.add("listBackground", new Texture(tempPixmap));
        tempPixmap.dispose();

        // 初始化Stage
        stage = new Stage();
        Gdx.input.setInputProcessor(stage);

        // 仅初始化一次列表
        list = new List<>(skin);
        String[] strings = {"Item 1", "Item 2", "Item 3", "Item 4", "Item 5"};
        list.setItems(strings);

        // 加入ScrollPane实现滚动
        scrollPane = new ScrollPane(list, skin);
        scrollPane.setFillParent(true);
        stage.addActor(scrollPane);
    }

    @Override
    public void update(float dt) {
        stage.act(dt);
    }

    @Override
    public void render(SpriteBatch sb) {
        Gdx.gl.glClearColor(1, 1, 1, 1);
        Gdx.gl.glBlendFunc(GL20.GL_SRC_ALPHA, GL20.GL_ONE_MINUS_SRC_ALPHA);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
        stage.draw();
    }

    @Override
    public void dispose() {
        stage.dispose();
        skin.dispose();
        atlas.dispose();
    }
}

内容的提问来源于stack exchange,提问作者h3vy

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.01 08:54:04