“RuntimeException: native typeface cannot be made” when loading font

后端 未结 9 1678
生来不讨喜
生来不讨喜 2020-12-05 09:35

I am attempting to use a custom font for a TextView on Android, following the guide here. Using the same font, same code, same everything, I get this in adb logcat:

相关标签:
9条回答
  • 2020-12-05 09:42

    FYI. My reason for the crash is some reason caused by Eclipse. All I did is just cleaning the project and ran again, then it works.

    Firstly, I tried the custom font in my test project which I use to try some new functions.It worked on the very first time. But it didn't work on the project i'm working on until I did as above.

    So try as many methods as you can.

    0 讨论(0)
  • 2020-12-05 09:47

    Android does support OTF files for Typefaces, if you're facing this problem, make sure that you're setting the right path for the font, for example, if you have the file fontname.otf, put it in a folder fonts inside assets folder and create the typeface like follows :

    Typeface typeface = Typeface.createFromAsset(getAssets(), "fonts/fontname.otf");
    

    (path argument should not start with "/") and the file name should not include special characters or a "-" and should be in lowercases

    0 讨论(0)
  • 2020-12-05 09:54

    Unfortunately, the typeface cannot be made error is not very specific, and it can be the result of many things going wrong. It's important to check for two things:

    • The first and most important: The file is found!
    • The font is valid in your device.

    The best way is to change your font file for a known valid font file.
    If it fails, then it's the first problem.
    If not, it's the second, so you will have to deal with FontForge or look for another font.

    0 讨论(0)
  • 2020-12-05 09:58

    check font's name and extension. it is case sensitive & probably all caps. eg.

    Typeface tf = Typeface.createFromAsset(getAssets(), "fonts/MOLOT.OTF")
    
    0 讨论(0)
  • 2020-12-05 10:00

    Android does support OTF files for Typefaces, if you're facing this problem, make sure that you're setting the right path for the font.put font into folder fonts inside assets folder and create the typeface as below :

    Typeface typeface = Typeface.createFromAsset(getAssets(), "font/StencilStd.otf");
    TextView text = (TextView) findViewById(R.id.textView);
    text.setTypeface(typeface);
    
    0 讨论(0)
  • 2020-12-05 10:02

    Android does not support OpenType (OTF), only TrueType (TTF), so your Molot.otf font probably will not work. I wrote both of those blog posts you link to in your opening sentence (the one is a pirated copy of the other), and they do not use Molot.otf.

    (BTW, I somewhat repaired the formatting of that post -- AndroidGuys keeps changing WordPress hosts, and so my older posts are terribly broken in terms of formatting).

    EDIT: As stated in the comments, Android DOES now support OTF.

    0 讨论(0)
提交回复
热议问题