LibGDX文本字段输入导致崩溃 [英] LibGDX textfield input causes crash

查看:49
本文介绍了LibGDX文本字段输入导致崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

So i'm trying to make a login screen, now the problem is, when I input text in the textfield, my game just crashes, this is my mainmenu class:

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();

    }


}

当我输入某些内容时,会出现此错误:

And when I input something I get this error:

    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)

我已经尝试了很多事情,但是我一直收到这个错误,希望有人可以帮助我.

I tried many things already but I keep getting this error, I hope someone can help me..

推荐答案

遇到了同样的问题,因此,这就是您的答案.

Was running into this same issue, so here's your answer.

TextFieldStyle.fontColor未设置为字体的颜色,您必须手动设置

TextFieldStyle.fontColor isn't set to the font's color, you must set it manually, in your case

txtStyle.fontColor = Color.WHITE;

这篇关于LibGDX文本字段输入导致崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆