LibGDX 文本字段输入导致崩溃

2024-04-10

所以我试图制作一个登录屏幕,现在的问题是,当我在文本字段中输入文本时,我的游戏崩溃了,这是我的主菜单类:

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.Texture.TextureFilter;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.badlogic.gdx.scenes.scene2d.ui.Label.LabelStyle;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.scenes.scene2d.ui.Table;
import com.badlogic.gdx.scenes.scene2d.ui.TextField;
import com.badlogic.gdx.scenes.scene2d.ui.TextField.TextFieldListener;
import com.badlogic.gdx.scenes.scene2d.ui.TextField.TextFieldStyle;

import eu.skillaria.client.GameClient;

public class MainMenu implements Screen {
    GameClient client;

    Texture background;
    Sprite backgroundSprite;
    SpriteBatch spriteBatch;
    Table table;
    Stage stage;

    TextureAtlas menuAtlas;
    Skin menuSkin;

    Label info;
    BitmapFont mainFont;
    Label lblUsername, lblPassword;
    TextField txtUsername, txtPassword;

    public MainMenu(GameClient client) {
        this.client = client;
    }

    @Override
    public void render(float delta) {

        spriteBatch.begin();
        backgroundSprite.draw(spriteBatch);
        spriteBatch.end();

        stage.act(Gdx.graphics.getDeltaTime());
        stage.draw();
    }

    @Override
    public void resize(int width, int height) {
        // TODO Auto-generated method stub

    }

    @Override
    public void show() {

        stage = new Stage(Gdx.graphics.getWidth(), Gdx.graphics.getHeight(), true);
        Gdx.input.setInputProcessor(stage);

        mainFont = new BitmapFont(Gdx.files.internal("data/fonts/main.fnt"), false);
        mainFont.setColor(1, 1, 1, 1);
        background = new Texture("data/images/background.png");
        background.setFilter(TextureFilter.Linear, TextureFilter.Linear);
        backgroundSprite = new Sprite(background);
        backgroundSprite.setPosition(0, 0);

        spriteBatch = new SpriteBatch();
        table = new Table();

        menuSkin = new Skin();
        menuAtlas = new TextureAtlas("data/packs/menu.pack");

        menuSkin.addRegions(menuAtlas);

        LabelStyle infoStyle = new LabelStyle(mainFont, Color.WHITE);

        info = new Label("Hello there and welcome.", infoStyle);
        info.setBounds(110, 355, 585, 90);
        info.setAlignment(2);

        lblUsername = new Label("Username:", infoStyle);
        lblPassword = new Label("Password:", infoStyle);


        TextFieldStyle txtStyle = new TextFieldStyle();
        txtStyle.background = menuSkin.getDrawable("textbox");
        txtStyle.font = mainFont;

        txtUsername = new TextField("", txtStyle);
        txtPassword = new TextField("", txtStyle);
        txtPassword.setPasswordMode(true);
        txtPassword.setPasswordCharacter('*');
        txtUsername.setMessageText("test");

        txtUsername.setTextFieldListener(new TextFieldListener() {

            @Override
            public void keyTyped(TextField textField, char key) {
                    Gdx.app.log("Skillaria", "" + key);
            }
        });

        txtPassword.setTextFieldListener(new TextFieldListener() {

            @Override
            public void keyTyped(TextField textField, char key) {

                    Gdx.app.log("Skillaria", "" + key);
            }
        });

        table.add(lblUsername).pad(2);
        table.row();
        table.add(txtUsername).pad(2);
        table.row().pad(10);
        table.add(lblPassword).pad(2);
        table.row();
        table.add(txtPassword).pad(2);
        table.setBounds(110, 225, 585, 200);

        stage.addActor(info);
        stage.addActor(table);
    }

    @Override
    public void hide() {
        // TODO Auto-generated method stub

    }

    @Override
    public void pause() {
        // TODO Auto-generated method stub

    }

    @Override
    public void resume() {
        // TODO Auto-generated method stub

    }

    @Override
    public void dispose() {
        spriteBatch.dispose();
        background.dispose();
        stage.dispose();
        menuSkin.dispose();
        menuAtlas.dispose();
        mainFont.dispose();

    }


}

当我输入一些内容时,我收到此错误:

    Skillaria: t
Exception in thread "LWJGL Application" java.lang.NullPointerException
    at com.badlogic.gdx.scenes.scene2d.ui.TextField.draw(TextField.java:514)
    at com.badlogic.gdx.scenes.scene2d.Group.drawChildren(Group.java:125)
    at com.badlogic.gdx.scenes.scene2d.Group.draw(Group.java:56)
    at com.badlogic.gdx.scenes.scene2d.ui.WidgetGroup.draw(WidgetGroup.java:150)
    at com.badlogic.gdx.scenes.scene2d.ui.Table.draw(Table.java:95)
    at com.badlogic.gdx.scenes.scene2d.Group.drawChildren(Group.java:111)
    at com.badlogic.gdx.scenes.scene2d.Group.draw(Group.java:56)
    at com.badlogic.gdx.scenes.scene2d.Stage.draw(Stage.java:185)
    at eu.skillaria.client.screen.MainMenu.render(MainMenu.java:52)
    at com.badlogic.gdx.Game.render(Game.java:46)
    at eu.skillaria.client.GameClient.render(GameClient.java:23)
    at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:207)
    at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:114)

我已经尝试了很多事情,但我不断收到此错误,我希望有人可以帮助我..


遇到了同样的问题,所以这就是你的答案。

TextFieldStyle.fontColor 未设置为字体的颜色,您必须根据您的情况手动设置它

txtStyle.fontColor = Color.WHITE;
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

LibGDX 文本字段输入导致崩溃 的相关文章

随机推荐

  • TensorFlow tf.group 忽略依赖关系?

    继从先前的问题 https stackoverflow com questions 44244275 tensorflow fifoqueue not fifo 它似乎tf group确实忽略了依赖关系 这是一个简单的独立示例 我已在 Py
  • 为什么 Chrome 在使用 HTTP/2 时会对请求进行排队?

    我有一个使用 HTTP 2 的网站 该网站加载图像的速度很慢 看看 Chrome 的 Devtools 大部分时间都花在 排队 图像的网络请求上 我的理解是 使用 HTTP 2 可以通过同一个 TCP 连接同时发出多个请求 但我看到 Chr
  • 超链接右侧带有图标的 JQuery UI

    我试图在超链接的右侧放置一个图标 使用 JQuery UI 主题 然而 我得到的最令人满意的结果是页面最右侧的图标 而不是紧接在实际文本之后 最简单的选择是有一个 img 标签位于文本后面 但图标需要根据当前主题设置样式 这就是我所拥有的
  • Zookeeper管理服务器端口

    在Windows上安装了zookeeper 3 5 6 bin 出现错误 无法启动AdminServer 异常退出 org apache zookeeper server admin AdminServer AdminServerExcep
  • 将变量从 Github Action 传递到 Docker 镜像构建

    我一直致力于设置 Github Actions 工作流程来构建 docker 映像 我需要将环境变量传递到图像中 以便我的 Django 项目能够正确运行 不幸的是 当我构建图像时 它没有收到变量的值 我的工作流程文件的相关部分 name
  • 如何通过索引列表过滤 numpy 数组?

    我有一个 numpy 数组 filtered rows 由 LAS 数据组成 x y z intensity classification 我创建了一个cKDTree点并找到最近的邻居 query ball point 这是该点及其邻居的索
  • webview_flutter 和 flutter_webview_plugin 哪个更好

    我已经在flutter中开发了web view 我不清楚哪个更好 webview flutter 与 flutter webview plugin In webview flutter Flutter 小部件可以在 Web 视图上显示 so
  • 如何制作Python模块或函数并在编写其他程序时使用它?

    在很多情况下 我必须在多个程序中一遍又一遍地编写大行代码 所以我想知道是否可以只编写一个程序 保存它 然后在不同的程序 例如函数或模块 中调用它 一个基本的例子 我编写一个程序来检查一个数字是否是回文 然后我想编写一个程序来检查一个数字是否
  • 强调 WordPress 主题 - 添加第二个侧边栏

    使用下划线主题 s 启动 WordPress 网站 我已经有一个侧边栏可以工作 但想在同一页面上制作第二个侧边栏 包含不同的小部件 我已将新的侧边栏添加到functions php 中 它出现在Wordpress 登录屏幕中 我可以将小部件
  • 存储对特定文件的更改

    我有一个大型 git 项目 我愚蠢地将其导入到 eclipse 并运行自动格式 现在 项目中的每个文件都显示为已修改 我宁愿还原所有仅格式化且未进行其他更改的文件 而不是提交格式化的文件 例如 git status On branch ma
  • Python 多处理和处理工人中的异常

    我使用 python 多处理库来实现一种算法 其中有许多工作人员处理某些数据并将结果返回给父进程 我使用 multiprocessing Queue 将作业传递给工作人员 然后收集结果 一切都运行得很好 直到工作人员无法处理某些数据块 在下
  • Opencv Python打开dng格式

    我不知道如何在 opencv 中打开 dng 文件 该文件是在使用三星 Galaxy S7 的专业选项时创建的 使用这些选项时创建的图像是 dng 文件以及尺寸为 3024 x 4032 的 jpg 我相信这也是 dng 文件的尺寸 我尝试
  • MVC3 和 EF 数据优先:最佳实践是什么?

    似乎 MVC3 和 EF4 1 的大部分焦点都围绕 代码优先 我似乎找不到任何满足以下条件的示例或教程 使用现有的 SQLServer 数据库 有单独的网络和数据访问项目 我们将有多个网络应用程序共享相同的数据访问类 验证建议 是否存在这样
  • javascript中的地理空间查询[关闭]

    Closed 这个问题正在寻求书籍 工具 软件库等的推荐 不满足堆栈溢出指南 help closed questions 目前不接受答案 我正在寻找一个 javascript 库 它可以让我进行地理空间查询 我知道 OpenLayers 和
  • codeigniter、result() 与 result_array()

    我都用result and result array 通常我喜欢将结果作为数组 这就是我主要使用 result array 的原因 但我想知道我应该遵循哪种更好的方法 就性能而言 其中哪一个使用起来更有效 这是我在 codeigniter
  • 如何使用VBA保存分号分隔的csv文件?

    我将数据复制到电子表格中 使用 VBA 对其进行格式化 然后将该工作表保存到 CSV 文件中 我使用以下代码 ws SaveAs Filename filestr Fileformat xlCSV ws 是我保存的工作表 这给了我一个以逗号
  • 在 Symfony2 中将多对多关系保存到数据库

    在我的 Symfony2 项目中 我有两个相关的实体 用户和收藏夹 他们之间是多对多的关系 我的应用程序的工作原理如下 在我的 Twig 页面中 我有一些带有 添加到收藏夹 按钮的项目 当您单击该按钮时 我的控制器会将 item id 保存
  • 无法播放 AVAudioPlayer 中文档中的文件

    我在应用程序的文档文件夹中有一个文件 我想播放它 if NSFileManager defaultManager fileExistsAtPath pathString let url NSURL fileURLWithPath pathS
  • Mac OSX 中的 eclipse 中凭证存储失败

    当我使用 mac 中的 eclipse 和我的凭据连接到 tfs 中的服务器项目时 它的连接没有任何问题 但是当我在登录对话框中检查 保存密码 时 它显示错误 凭据存储失败 无法保存您的凭据 有什么办法可以摆脱这个 因为我不想每次打开 ec
  • LibGDX 文本字段输入导致崩溃

    所以我试图制作一个登录屏幕 现在的问题是 当我在文本字段中输入文本时 我的游戏崩溃了 这是我的主菜单类 import com badlogic gdx Gdx import com badlogic gdx Screen import co