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)
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!
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
it covers how to add the jars and everything
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:
来源:https://stackoverflow.com/questions/19877955/libgdx-freetypefontgenerator-nosuchfield-exception