User interface 奇怪的libgdx TextField ui呈现错误(IndexOutOfBoundsException)

User interface 奇怪的libgdx TextField ui呈现错误(IndexOutOfBoundsException),user-interface,libgdx,render,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

当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

我希望你能原谅我对这个老问题的指责,尤其是在已经有答案的情况下。这个页面是谷歌上很少提及的主题之一,我发现了一个不同的解决方案

简而言之,我必须打开我正在使用的.fnt文件并删除对glyph 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 

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


这个GitHub问题似乎解决了问题的根源:

正如您所说,看起来像是并发或线程问题,请尝试在此处发布相关代码。可能重复的代码我会检查出来……这对我有什么帮助?我只是使用普通文本字段。当新行被删除后,这个问题似乎就消失了。但是我不明白如何解决文本字段的问题…?如果你要包含相关代码,这个问题可以进一步解决。
chars count=96
char id=32      x=0    y=0    width=0    height=0    xoffset=-1   yoffset=0    xadvance=9    page=0    chnl=0