所以我正在尝试制作一个登录屏幕,现在的问题是,当我在文本字段中输入文本时,我的游戏就崩溃了,这是我的主菜单类:
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)
我已经尝试了很多东西,但我一直收到这个错误,我希望有人能帮助我..