2013-10-09 3 views
3

Так я пытаюсь сделать экран входа в систему, теперь проблема, когда я ввод текста в текстовое поле, моя игра просто падает, это мой MainMenu класс:LibGDX вход TextField вызывает сбой

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) 

Я пытался много вещей уже, но я получаю эту ошибку, я надеюсь, что кто-то может мне помочь ..

+0

Какой номер строки 52 в коде? –

+0

52 - stage.draw(); – Stefan

+0

Номера строк могут не совпадать (зависит от того, какую версию Libgdx вы используете), но это строка 514 в TextField. Похоже, он ожидает установки fontColor. https://github.com/libgdx/libgdx/blob/master/gdx/src/com/badlogic/gdx/scenes/scene2d/ui/TextField.java#L514 –

ответ

5

был запущен в этот же вопрос, так что здесь ты РА nswer.

TextFieldStyle.fontColor не установлен цветом шрифта, вы должны установить его вручную, в вашем случае

txtStyle.fontColor = Color.WHITE; 
Смежные вопросы