当前位置: 首页 > 面试题库 >

加载字体时出现“ RuntimeException:无法生成本机字体”

阮阳曦
2023-03-14
问题内容

我尝试按照此处的指南,为Android上的TextView使用自定义字体。使用相同的字体,相同的代码,相同的所有内容,我在adb logcat中得到此信息:

W/dalvikvm(  317): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
E/AndroidRuntime(  317): FATAL EXCEPTION: main
E/AndroidRuntime(  317): java.lang.RuntimeException: Unable to start activity  ComponentInfo{org.evilx.quacklock/org.evilx.quacklock.MainActivity}:             java.lang.RuntimeException: native typeface cannot be made
E/AndroidRuntime(  317):        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
E/AndroidRuntime(  317):        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
E/AndroidRuntime(  317):        at android.app.ActivityThread.access$2300(ActivityThread.java:125)
E/AndroidRuntime(  317):        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
E/AndroidRuntime(  317):        at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime(  317):        at android.os.Looper.loop(Looper.java:123)
E/AndroidRuntime(  317):        at android.app.ActivityThread.main(ActivityThread.java:4627)
E/AndroidRuntime(  317):        at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(  317):        at java.lang.reflect.Method.invoke(Method.java:521)
E/AndroidRuntime(  317):        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
E/AndroidRuntime(  317):        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
E/AndroidRuntime(  317):        at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime(  317): Caused by: java.lang.RuntimeException: native typeface cannot be made
E/AndroidRuntime(  317):        at android.graphics.Typeface.<init>(Typeface.java:147)
E/AndroidRuntime(  317):        at android.graphics.Typeface.createFromAsset(Typeface.java:121)
E/AndroidRuntime(  317):        at org.evilx.quacklock.MainActivity.onCreate(MainActivity.java:24)
E/AndroidRuntime(  317):        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
E/AndroidRuntime(  317):        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
E/AndroidRuntime(  317):        ... 11 more
W/ActivityManager(   59):   Force finishing activity org.evilx.quacklock/.MainActivity
W/ActivityManager(   59): Activity pause timeout for HistoryRecord{43e80368 org.evilx.quacklock/.MainActivity}
D/dalvikvm(  247): GC_EXPLICIT freed 711 objects / 53160 bytes in 20922ms

我使用的是Molot.otf字体,该字体已在其中一个博客中成功使用。我还使用predator.ttf,这是另一种自定义字体,但格式为TrueType。

相关代码:

public class MainActivity extends Activity {
    // Called when the activity is first created.
    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        Typeface tf = Typeface.createFromAsset(getAssets(), "fonts/Molot.otf");
        TextView tv = (TextView) findViewById(R.id.CustomFontText);
        tv.setTypeface(tf);
    }
}

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <TextView
        android:id="@+id/CustomFontText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="30sp"
        android:text="Here is some text.">
    </TextView>
</LinearLayout>

是什么原因造成的?它对博客中的人有用,那为什么不呢?API中是否发生了一些重大更改,从而阻止了我这样做?


问题答案:

Android不支持OpenType(OTF),仅支持TrueType(TTF),因此您的Molot.otf字体可能无法使用。我在开篇中都写了两个您链接到的博客文章(一个是另一个的盗版副本),并且没有使用Molot.otf

(顺便说一句,我在某种程度上修复了该帖子的格式-AndroidGuys不断更改WordPress主机,因此我的旧帖子在格式方面被严重破坏了)。

编辑 :如评论所述,Android DOES 现在支持OTF。



 类似资料:
  • 问题内容: 我正在尝试使用在互联网上找到的字体,但是问题是我得到了带有“无法制作本机字体”的FC。 这是我的ListVIew的getView中的代码: 谁能告诉我为什么我可以使用自定义rom?您可以在这里获取它..文件是.ttf 问题答案: 字体文件由于某种原因损坏或不受支持。您可以将其拖放到SD卡上并从文件中加载,以确保资产没有问题。

  • 我在使用Tomcat加载字体时有这样的错误: %1无法解码下载的字体:https://my-address.com/css/fonts/robotocondensed-bold-webfont.woff2 1 OTS分析错误:无法将WOFF 2.0字体转换为SFNT %1无法解码下载的字体:https://my-address.com/css/fonts/robotocondensed-regul

  • 我目前正在android studio 3.0.1中开发一个支持cpp的android库。因此,我从一个小测试开始检查功能,并得到以下错误: 我的源文件是: TestProject/app/src/java/com/example/TestClass。Java语言 TestProject/app/src/cpp/native-lib。cpp公司 我还在TestProject/gradle中添加了“

  • 我得到这样的错误消息:线程“awt-eventqueue-0”org.apache.jena.riot.riotexception:[line:1,col:1]Prolog中不允许内容。当我试图通过NetBeans用java加载本地本体文件时?请问这是什么意思,我该怎么做?

  • 我使用npm下载了FontAwcle,然后使用grunts复制任务将css文件和字体复制到我的电子应用程序的root diretory中的正确文件夹中。 到目前为止还不错。一切都在它应该在的地方。 现在,当我在我的应用程序中引用FontAwgood时,图标不会加载。这些是我在控制台中遇到的错误: 无法解码下载的字体: file:///path/to/fonts/fontawesome-webfon