donc je suis en train de faire un écran de connexion, maintenant le problème est, quand je texte d'entrée dans le champ de texte, mon jeu se bloque juste, c'est ma classe mainmenu:LibGDX entrée textfield provoque accident
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();
}
}
et quand je entrée quelque chose que je reçois cette erreur:
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)
J'ai essayé beaucoup de choses déjà, mais je continue à obtenir cette erreur, j'espère que quelqu'un peut me aider ..
Quel est le numéro de la ligne 52 dans votre code? –
52 est stage.draw(); – Stefan
Les numéros de ligne peuvent ne pas correspondre (cela dépend de la version de Libgdx que vous utilisez), mais il s'agit de la ligne 514 dans TextField. On dirait qu'il attend un fontColor à définir. https://github.com/libgdx/libgdx/blob/master/gdx/src/com/badlogic/gdx/scenes/scene2d/ui/TextField.java#L514 –