试图加载“font.TTF”会导致“错误读取文件”“找不到文件”

时间:2015-03-07 19:58:12

标签: android android-studio libgdx

我正在尝试在libgdx中创建一个游戏,我想加载一个ttf字体并在屏幕上绘制一些文本,但它在我的Android设备上不起作用,我得到Unfortunately, game has stopped。我使用Android Studio作为IDE。 它适用于桌面。

这是我加载字体的方式:

FreeTypeFontGenerator generator = new FreeTypeFontGenerator(Gdx.files.internal("font.ttf"));

我在logcat中遇到以下错误:

03-07 20:41:44.902    9759-9779/com.mygdx.game.android E/AndroidRuntime﹕ FATAL EXCEPTION: GLThread 5345
    com.badlogic.gdx.utils.GdxRuntimeException: Error reading file: font.ttf (Internal)
            at com.badlogic.gdx.backends.android.AndroidFileHandle.read(AndroidFileHandle.java:77)
            at com.badlogic.gdx.files.FileHandle.readBytes(FileHandle.java:222)
            at com.badlogic.gdx.graphics.g2d.freetype.FreeType$Library.newFace(FreeType.java:69)
            at com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator.<init>(FreeTypeFontGenerator.java:106)
            at com.mygdx.game.MyGdxGame.create(MyGdxGame.java:42)
            at com.badlogic.gdx.backends.android.AndroidGraphics.onSurfaceChanged(AndroidGraphics.java:241)
            at android.opengl.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:1513)
            at android.opengl.GLSurfaceView$GLThread.run(GLSurfaceView.java:1248)
     Caused by: java.io.FileNotFoundException: font.ttf
            at android.content.res.AssetManager.openAsset(Native Method)
            at android.content.res.AssetManager.open(AssetManager.java:315)
            at android.content.res.AssetManager.open(AssetManager.java:289)
            at com.badlogic.gdx.backends.android.AndroidFileHandle.read(AndroidFileHandle.java:75)
            at com.badlogic.gdx.files.FileHandle.readBytes(FileHandle.java:222)
            at com.badlogic.gdx.graphics.g2d.freetype.FreeType$Library.newFace(FreeType.java:69)
            at com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator.<init>(FreeTypeFontGenerator.java:106)
            at com.mygdx.game.MyGdxGame.create(MyGdxGame.java:42)
            at com.badlogic.gdx.backends.android.AndroidGraphics.onSurfaceChanged(AndroidGraphics.java:241)
            at android.opengl.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:1513)
            at android.opengl.GLSurfaceView$GLThread.run(GLSurfaceView.java:1248)

这是文件的位置:

这是我的build.gradle文件 enter image description here

1 个答案:

答案 0 :(得分:8)

尝试:

... Gdx.files.internal("font.TTF"));

Android文件系统区分大小写。