Java 在棒棒糖上找不到字体资源 helvetica.ttf

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26986932/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 03:36:59  来源:igfitidea点击:

Font asset not found helvetica.ttf on lollipop

javaandroidfontsandroid-5.0-lollipop

提问by Sloganho

For some reason on lollipop, I'm getting this error and since lollipop is so new I can't find out why anywhere and I have no idea how to figure it out. Thanks in advance Logcat error:

出于某种原因,关于棒棒糖,我收到了这个错误,因为棒棒糖是如此新,我无法在任何地方找出原因,我也不知道如何弄清楚。提前致谢 Logcat 错误:

java.lang.RuntimeException: Unable to start activity                                       
ComponentInfo{com.bent.MissionaryTracker/com.bent.MissionaryTracker.MainActivity}:          
java.lang.RuntimeException: Font asset not found helvetica.ttf
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
at android.app.ActivityThread.access0(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Caused by: java.lang.RuntimeException: Font asset not found helvetica.ttf
at android.graphics.Typeface.createFromAsset(Typeface.java:190)
at com.bent.MissionaryTracker.MainActivity.onCreate(MainActivity.java:57)
at android.app.Activity.performCreate(Activity.java:5933)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
... 10 more

EDIT: HERE IS THE CODE THAT THROWS THE ERROR

编辑:这是引发错误的代码

    title = (TextView) findViewById(R.id.title);


    Typeface font = Typeface.createFromAsset(getAssets(), "helvetica.ttf");
    title.setTypeface(font);

I have helvetica.ttf in my assets folder in my project folders.

我的项目文件夹中的资产文件夹中有 helvetica.ttf。

EDIT: This app works on all devices up until 5.0 so for some reason 5.0 is not recognizing the file in my assets folder.

编辑:此应用程序适用于 5.0 之前的所有设备,因此由于某种原因,5.0 无法识别我的资产文件夹中的文件。

I tried to post a screenshot of it in my assets folder but I don't have enough reputation to post images.

我试图在我的资产文件夹中发布它的屏幕截图,但我没有足够的声誉来发布图像。

采纳答案by Sagar Devanga

If you are working on Android Studio make sure your asset folder is under main and not res This worked for me

如果您在 Android Studio 上工作,请确保您的资产文件夹位于 main 而不是 res 这对我有用

回答by Sileria

Ok Guys,

好,朋友们,

I ran into the same problem. First of all I made my font loading logic crash proof:

我遇到了同样的问题。首先,我制作了字体加载逻辑防崩溃:

/**
 * Init method. This method must be called before any other call in this class.
 */
public static void init () {
    fontPlain  = createFont( "helvetica.ttf" , Typeface.NORMAL );
    fontBold   = createFont( "helveticab.ttf", Typeface.BOLD   );
}

/**
 * Load font from assets font folder.
 */
public static Typeface createFont (String font, int style) {
    Typeface typeface;
    try {
        AssetManager assets = Kit.getAppContext().getAssets();
        typeface = Typeface.createFromAsset( assets, "fonts" + File.separator + font );
    }
    catch (RuntimeException e) {
        // createFromAsset() will throw a RuntimeException in case of error.
        Log.e( Constants.TAG, "Unable to create font: " + font, e );
        typeface = Typeface.defaultFromStyle( style );
    }
    return typeface;
}

And then I also downloaded another font from this project in git:

然后我还在 git 中从这个项目下载了另一种字体:

https://github.com/dimanchec3/Tutoriapps-Android/blob/master/assets/Helvetica.ttf

https://github.com/dimanchec3/Tutoriapps-Android/blob/master/assets/Helvetica.ttf

I realized that my old font was 56KB and this new font is 84KB. I guess I had an older version of the font.

我意识到我的旧字体是 56KB,而这个新字体是 84KB。我想我有一个旧版本的字体。

Please make sure you have the proper license before using this font.

在使用此字体之前,请确保您拥有正确的许可证。

回答by Nublodeveloper

If changing the directory doesn't work it could be due to a problem already fixed by the Android team but didn't make it in time for 5.0. It's all explained here:

如果更改目录不起作用,则可能是由于 Android 团队已经修复了一个问题,但没有及时解决 5.0.1 的问题。这一切都在这里解释:

https://code.google.com/p/android-developer-preview/issues/detail?id=608&thanks=608&ts=1404735239

https://code.google.com/p/android-developer-preview/issues/detail?id=608&thanks=608&ts=1404735239

The workaround (at least it was for me) is to fix the font files with TTX as mentioned in the linked issue.

解决方法(至少对我来说是这样)是使用链接问题中提到的 TTX 修复字体文件。

Getting TTX to work can be a bit of a hassle if you have no python expertise so I ended up downloading Adobe's AFDKOthat includes TTX. You have to run 'ttx yourfontfile.ttf' that'll turn yourfile to .ttx and then run it again 'ttx yourfontfile.ttx' to get a new .ttf that works with 5.0.

如果您没有 Python 专业知识,让 TTX 工作可能会有点麻烦,所以我最终下载了包含 TTX 的Adobe AFDKO。您必须运行'ttx yourfontfile.ttf',它将您的文件转换为.ttx,然后再次运行'ttx yourfontfile.ttx' 以获得适用于5.0 的新.ttf。

回答by Leebeedev

I faced this issue before. I deleted x.ttf and then undid the deletion, tried to run with no lock. So my fix was just to delete it and downloaded the font again and pasted it to fonts/ folder and every thing worked fine.

我以前遇到过这个问题。我删除了 x.ttf 然后取消删除,尝试无锁运行。所以我的修复只是删除它并再次下载字体并将其粘贴到 fonts/ 文件夹,一切正常。

回答by Jorge Casariego

This worked for me. In my case it didn't work because I created assets folder in res and not in main.

这对我有用。就我而言,它不起作用,因为我在 res 而不是在 main 中创建了资产文件夹。

enter image description here

在此处输入图片说明

回答by Aman Goel

Just place a assets folder inside main directory. After do this:

只需在主目录中放置一个资产文件夹。执行此操作后:

 Typeface typeface=Typeface.createFromAsset(getAssets(),"OpenSans-Light.ttf");
    textView.setTypeface(typeface);

回答by keredniy

I had this problem too before I realized I didn't put "fonts/" before the .otf file

在我意识到我没有在 .otf 文件之前放置“fonts/”之前,我也遇到了这个问题

 Typeface font = Typeface.createFromAsset(getAssets(),"fonts/TTMastersDEMOBlack.ttf");

回答by keybee

Clean the project, it helped me on android 6.0

清理项目,它在 android 6.0 上帮助了我

回答by Penzzz

Now it's better to use for custom fonts this approach

现在最好将这种方法用于自定义字体

Store fonts in res/font

res/font 中存储字体

To get typeface use :

要获得字体使用:

Typeface typeface = ResourcesCompat.getFont(context, R.font.myfont);