奇怪的libgdx TextField ui渲染错误(IndexOutOfBoundsException)

时间:2015-09-25 15:59:59

标签: user-interface libgdx render

我在libgdx ui渲染时偶尔会出现这个错误:

09-25 17:50:26.779  31171-32670/se.company.app.android E/AndroidRuntime﹕ FATAL EXCEPTION: GLThread 1126
Process: se.company.app.android, PID: 31171
java.lang.IndexOutOfBoundsException: index can't be >= size: 36 >= 36
        at com.badlogic.gdx.utils.FloatArray.get(FloatArray.java:104)
        at com.badlogic.gdx.scenes.scene2d.ui.TextField.calculateOffsets(TextField.java:214)
        at com.badlogic.gdx.scenes.scene2d.ui.TextArea.calculateOffsets(TextArea.java:266)
        at com.badlogic.gdx.scenes.scene2d.ui.TextField.draw(TextField.java:294)
        at com.badlogic.gdx.scenes.scene2d.Group.drawChildren(Group.java:123)
        at com.badlogic.gdx.scenes.scene2d.Group.draw(Group.java:57)
        at com.badlogic.gdx.scenes.scene2d.ui.WidgetGroup.draw(WidgetGroup.java:154)
        at com.badlogic.gdx.scenes.scene2d.ui.Table.draw(Table.java:117)
        at com.badlogic.gdx.scenes.scene2d.Group.drawChildren(Group.java:110)
        at com.badlogic.gdx.scenes.scene2d.Group.draw(Group.java:57)
        at com.badlogic.gdx.scenes.scene2d.Stage.draw(Stage.java:128)
        at se.company.app.screens.AbstractScreen.render(AbstractScreen.java:36)
        at se.company.app.screens.InstructionsScreen.render(InstructionsScreen.java:80)
        at com.badlogic.gdx.Game.render(Game.java:46)
        at com.badlogic.gdx.backends.android.AndroidGraphics.onDrawFrame(AndroidGraphics.java:422)
        at android.opengl.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:1522)
        at android.opengl.GLSurfaceView$GLThread.run(GLSurfaceView.java:1239)
09-25 17:50:30.823  31171-31171/se.company.app.android E/AndroidGraphics﹕ waiting for pause synchronization took too long; assuming deadlock and killing
09-25 17:50:30.833  31171-31171/se.company.app.android I/Process﹕ Sending signal. PID: 31171 SIG: 9

我无法弄清楚它是什么。线程问题?但是什么?当我设置文本时,我使用Gdx.app.postRunnable()。

我有课程:

public class InstructionsScreen extends AbstractScreen {

private InputProcessor inputProcessor = new InputAdapter() {
    @Override
    public boolean keyDown(int keycode) {
        if ((keycode == Input.Keys.ESCAPE) || (keycode == Input.Keys.BACK) ) {
            game.setScreen(new BeginGameOptionsScreen(game, resources));
        }
        return false;
    }
};

private Table table;
private ScrollPane resultsTextAreaScrollPane;
private TextArea resultsTextArea;

private TextArea textArea;
private float maxX,maxY,maxZ;
private Sound sound;
private long timer=0;

public InstructionsScreen(GameClass game, Resources resources) {
    super(game, resources);
}

@Override
public void resize(int width, int height) {

    addInputProcessor(inputProcessor);

    table = new Table();
    table.setFillParent(true);
    stage.addActor(table);

    TextArea resultsTextAreaBool;
    resultsTextAreaBool = new TextArea("", resources.getSkin());
    resultsTextAreaBool.setDisabled(true);
    resultsTextAreaBool.setText(String.valueOf(Gdx.input.isPeripheralAvailable(Input.Peripheral.Accelerometer)));
    table.add(resultsTextAreaBool).width(GraphicUtils.widthOfScreen(90)).height(GraphicUtils.heightOfScreen(20)).pad(GraphicUtils.onePercentHeight, GraphicUtils.onePercentWidth, GraphicUtils.onePercentHeight, GraphicUtils.onePercentWidth);
    table.row();

    resultsTextArea = new TextArea("", resources.getSkin());
    resultsTextArea.setDisabled(true);
    resultsTextArea.setText("");
    table.add(resultsTextArea).width(GraphicUtils.widthOfScreen(90)).height(GraphicUtils.heightOfScreen(40)).pad(GraphicUtils.onePercentHeight, GraphicUtils.onePercentWidth, GraphicUtils.onePercentHeight, GraphicUtils.onePercentWidth);
    table.row();

    sound=resources.getRobotBlip();
}

@Override
public void render(float delta) {
    super.render(delta);

    float accelX = Gdx.input.getAccelerometerX();
    float accelY = Gdx.input.getAccelerometerY();
    float accelZ = Gdx.input.getAccelerometerZ();

    if(accelX>maxX)
        maxX=accelX;
    if(accelY>maxY)
        maxY=accelY;
    if(accelZ>maxZ)
        maxZ=accelZ;

    if(Math.abs(maxX)>19f) {
        timer++;
    }

    if(timer==1) {
        sound.play();
    }

    if(timer>20) {
        timer=0;
        maxX=0;
    }

    Gdx.app.postRunnable(new Runnable() {
        @Override
        public void run() {
            resultsTextArea.setText("x=" + String.valueOf(maxX) + "\ny=" + String.valueOf(maxY) + "\nz=" + String.valueOf(maxZ));
        }
    });

}

}

由于换行符而出现问题" \ n"在上面的字符串。 我使用FreeTypeFontGenerator动态创建字体:

    FreeTypeFontGenerator generator = new FreeTypeFontGenerator(Gdx.files.internal("data/fonts/anonymous-pro/Anonymous_Pro.ttf"));
    FreeTypeFontGenerator.FreeTypeFontParameter freeTypeFontParameter = new FreeTypeFontGenerator.FreeTypeFontParameter();
    freeTypeFontParameter.borderColor = Color.BLACK;
    freeTypeFontParameter.borderWidth = 1;

    freeTypeFontParameter.size = (int)(Gdx.graphics.getWidth()/25.0f); 

    anonymous_pro = generator.generateFont(freeTypeFontParameter);
    generator.dispose();

我添加了:

    TextField.TextFieldStyle textFieldStyle=skin.get("default", TextField.TextFieldStyle.class);
    textFieldStyle.font=anonymous_pro;
    skin.add("default", textFieldStyle);

并检索:

resultsTextArea = new TextArea("", resources.getSkin()); //Resources is my own class

1 个答案:

答案 0 :(得分:0)

我希望你能帮我解决这个老问题,特别是在已经有答案的情况下。这个页面是谷歌关于主题的极少引用之一,我发现了一个不同的解决方案。

简而言之,我不得不打开我正在使用的.fnt文件并删除对字形0和10的引用。我知道您正在使用FreeTypeFontGenerator,但我怀疑它是否会生成字形大小对于字符代码0和10也是如此。对于使用Hiero或其他生成器的人,您可以编辑.fnt文件并更改以下行:

chars count=98
char id=0       x=0    y=0    width=0    height=0    xoffset=-1   yoffset=0    xadvance=9    page=0    chnl=0 
char id=10      x=0    y=0    width=0    height=0    xoffset=-1   yoffset=0    xadvance=0    page=0    chnl=0 
char id=32      x=0    y=0    width=0    height=0    xoffset=-1   yoffset=0    xadvance=9    page=0    chnl=0 

chars count=96
char id=32      x=0    y=0    width=0    height=0    xoffset=-1   yoffset=0    xadvance=9    page=0    chnl=0 

请注意,id = 0和id = 10已消失且计数已更新。

此GitHub问题似乎解决了问题的根源:https://github.com/libgdx/libgdx/issues/4570