加载字体时崩溃 [英] Crash when loading font

查看:151
本文介绍了加载字体时崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在尝试使用自定义字体在Android上一个TextView,下面这里 >和这里。用同样的字体,同样的code,同样的一切,我得到这个在亚行logcat:

I am attempting to use a custom font for a TextView on Android, following the guide here and here. Using the same font, same code, same everything, I get this in 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格式

Hmm... okay. I'm using the font Molot.otf, which was successfully used in one of the blogs. I'm also using predator.ttf, another custom font but in TrueType format

相关code:

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,$ P $从这样做呢?

What would be causing this? It worked for the people in the blogs, so why not me? Did something significant change in the API that's preventing me from doing this?

推荐答案

Android不支持OpenType字体(OTF),仅TrueType字体(TTF),这样你的 Molot.otf 字体可能不会工作。我写了两个你在开头一句链接到这些博客文章(一个是另一个的盗版),他们不使用 Molot.otf

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.

(顺便说一句,我有点修复该职位的格式 - AndroidGuys不断变化的Word preSS主机,所以我的旧帖子在格式方面非常碎)

(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).

修改:正如评论指出,Android的 DOES 现在支持OTF

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

这篇关于加载字体时崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆