奇怪的libgdx TextField UI渲染错误(IndexOutOfBoundsException) [英] Strange libgdx TextField ui render error (IndexOutOfBoundsException)

查看:103
本文介绍了奇怪的libgdx TextField UI渲染错误(IndexOutOfBoundsException)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当libgdx ui渲染时,偶尔会出现此错误:

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

我不知道这是什么.线程问题?但是呢设置文字时,我可以使用Gdx.app.postRunnable()来实现.

I can't figure out what it is about. Threading issue? But what? When I set text I do it with 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));
        }
    });

}

}

问题似乎是由于上面的String中的换行符"\ n"引起的. 我使用FreeTypeFontGenerator即时创建字体:

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

我添加以下内容:

    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

推荐答案

我希望您能原谅我提出的这个旧问题,尤其是在已经有答案的情况下.此页面是Google在该主题上很少提及的内容之一,我找到了另一种解决方案.

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.

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

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 

收件人

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已消失,计数已更新.

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

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

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

这篇关于奇怪的libgdx TextField UI渲染错误(IndexOutOfBoundsException)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆