加载字体时,"RuntimeException:无法生成本机字体"

The*_*eLQ 64 java android android-fonts runtimeexception

我试图在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
Run Code Online (Sandbox Code Playgroud)

我使用的是字体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);
    }
}
Run Code Online (Sandbox Code Playgroud)

<?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>
Run Code Online (Sandbox Code Playgroud)

会导致什么?它适用于博客中的人们,为什么不呢?在API中发生了哪些重大变化阻止我这样做?

Com*_*are 48

Android不支持OpenType(OTF),只支持TrueType(TTF),因此您的Molot.otf字体可能无法正常工作.我在你的开头句中写了你链接到的那些博客文章(一个是另一个的盗版副本),但他们没有使用Molot.otf.

(顺便说一句,我有点修复了那篇帖子的格式--AndroidGuys不断改变WordPress主机,因此我的旧帖子在格式方面非常糟糕).

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

  • 这个答案已经过时了......我已经使用android的字体成功渲染了OTF字体 (11认同)

小智 27

我也得到了同样的错误,我有解决方案.

Typeface tf = Typeface.createFromAsset(getAssets(), "fonts/Molot.otf")
Run Code Online (Sandbox Code Playgroud)

你必须把fonts/Molot.otfassets/fonts 文件夹中的Eclipse.

你可以运行它.

如果你不能成功运行它你可以通过发送proplem


mam*_*uso 10

不幸的是,字体不能被制作错误不是很具体,它可能是许多事情出错的结果.检查两件事很重要:

  • 第一个也是最重要的:找到了文件!
  • 该字体在您的设备中有效.

最好的方法是更改​​已知有效字体文件的字体文件.
如果它失败了,那么这是第一个问题.
如果没有,那就是第二个,所以你将不得不处理FontForge或寻找另一种字体.


mar*_*eng 9

检查字体的名称和扩展名.它区分大小写并且可能全部大写.例如.

Typeface tf = Typeface.createFromAsset(getAssets(), "fonts/MOLOT.OTF")
Run Code Online (Sandbox Code Playgroud)


Mic*_*lle 6

仅供参考.我崩溃的原因是由Eclipse引起的一些原因.我所做的只是清理项目并再次运行,然后就可以了.

首先,我在我的测试项目中尝试了自定义字体,我用它来尝试一些新的功能.它是第一次工作.但它在我正在进行的项目上没有奏效,直到我如上所述.

所以尝试尽可能多的方法.


Hit*_*iya 6

Android确实支持Typefaces的OTF文件,如果您遇到此问题,请确保将font.put字体的正确路径设置为assets文件夹内的文件夹字体,并创建如下字体:

Typeface typeface = Typeface.createFromAsset(getAssets(), "font/StencilStd.otf");
TextView text = (TextView) findViewById(R.id.textView);
text.setTypeface(typeface);
Run Code Online (Sandbox Code Playgroud)