Strange libgdx TextField ui render error (IndexOut

2019-08-10 05:17发布

I get this error just once in a while when libgdx ui renders:

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

I can't figure out what it is about. Threading issue? But what? When I set text I do it with Gdx.app.postRunnable().

I have classes:

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

}

}

Problem seems to arise because of newlines "\n" in String above. I use FreeTypeFontGenerator for font creation on the fly:

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

Which I add with:

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

And retrieve with:

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

1条回答
Bombasti
2楼-- · 2019-08-10 05:53

I hope you'll excuse me for bumping this old issue, especially when there's already an answer. This page is one of very few references on Google to the subject matter and I found a different solution.

In short, I had to open up the .fnt file that I was using and remove the references to glyphs 0 and 10. I know you're using FreeTypeFontGenerator, but I suspect it's generating Glyph sizes for character codes 0 and 10, too. For the folks using Hiero or another generator, you can edit the .fnt file and change the following lines:

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 

To

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

Note that id=0 and id=10 are gone and the count is updated.

This GitHub issue appears to address the root of the problem: https://github.com/libgdx/libgdx/issues/4570

查看更多
登录 后发表回答