LibGDX FreeTypeFontGenerator NoSuchField exception

元气小坏坏 提交于 2019-12-08 02:59:49

问题


Right now, I'm building a small game with LibGDX in Java, and I want to use a TTF font. I've added gdx-freetype.jar and gdx-freetype-natives.jar to my build paths, but when I get to running my application, I get a "java.lang.NoSuchFieldError: id" error. The code responsible:

FreeTypeFontGenerator generator = new 
FreeTypeFontGenerator(Gdx.files.internal("data/Prosto.ttf"));
BitmapFont font = generator.generateFont(12);
generator.dispose();

I read somewhere it might have something to do with the versions of the JARs. I've tried running the setup UI again, I've tried JARs from another version, but to no avail.

EDIT: Here's the complete stacktrace:

Exception in thread "LWJGL Application" com.badlogic.gdx.utils.GdxRuntimeException:    java.lang.NoSuchFieldError: id
at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:116)
Caused by: java.lang.NoSuchFieldError: id
at          com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator.generateData(FreeTypeFontGenerator.java:288)
at    com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator.generateFont(FreeTypeFontGenerator.java:137)
at com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator.generateFont(FreeTypeFontGenerator.java:148)
at com.serialbit.personal.MainMenu.create(MainMenu.java:44)
at com.serialbit.personal.MainMenu.<init>(MainMenu.java:32)
at com.serialbit.personal.Tyredus.create(TyredusGame.java:8)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:130)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:110)

回答1:


Faced similar problem today - you are using new version of freetype extension without updating the libgdx!

The commit to libgdx with name "Adding multi-page fonts to BitmapFont; updating FreeTypeFontGenerator..." added usage of Glyph.id field in FreeTypeFontGenerator.

Glyph class is inside BitmapFont of libgdx. So without updating libgdx - it produces this exception.

You should update libgdx also - this will solve this issue. Or use older version of freetype extension!




回答2:


use this, worked for me

FreeTypeFontGenerator gen = new FreeTypeFontGenerator(Gdx.files.internal("data/Prosto.tff"));
       font=gen.generateFont(12);
       font.getRegion().getTexture().setFilter(TextureFilter.Linear,TextureFilter.Linear);

here is a really good tutorial on fonts for LIBGDX. i followed it myself and got no errors

https://www.google.co.uk/url?sa=t&rct=j&q=&esrc=s&source=web&cd=1&cad=rja&ved=0CDcQtwIwAA&url=http%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3D3JgjFZFQu74&ei=E-R_UrrKGcmrhQf1l4GYBQ&usg=AFQjCNFDDpCbZRhftx7aXKmdcturr9qpDw&sig2=bY_ZJZw-DIgdNObDaXg3EA&bvm=bv.56146854,d.ZG4

it covers how to add the jars and everything




回答3:


The problem is caused by your not using an instance of FreeTypeFontParameter. So when it goes to generate the font, it doesn't have a set of attributes to assign to it. AFAIK, this is following the principles of the Lazy Initialization:

http://en.wikipedia.org/wiki/Lazy_initialization



来源:https://stackoverflow.com/questions/19877955/libgdx-freetypefontgenerator-nosuchfield-exception

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!