我正在尝试按照指南here在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中发生了哪些重大变化阻止我这样做?
答案 0 :(得分:48)
Android不支持OpenType(OTF),只支持TrueType(TTF),因此您的Molot.otf
字体可能无效。我在你的开头句中写了你链接到的那些博客文章(一个是另一个的盗版副本),他们不使用Molot.otf
。
(顺便说一句,我有点修复了那篇帖子的格式--AndroidGuys不断改变WordPress主机,所以我的旧帖子在格式化方面非常糟糕。)
编辑:正如评论中所述,Android DOES 现在支持OTF。
答案 1 :(得分:27)
我也得到了同样的错误,我有解决方案。
Typeface tf = Typeface.createFromAsset(getAssets(), "fonts/Molot.otf")
您必须将fonts/Molot.otf
放入Eclipse中的assets/fonts
文件夹中。
你可以运行它。
如果你不能成功运行它,你可以通过
发送proplem答案 2 :(得分:10)
不幸的是, 字体无法生成 错误不是很具体,而且可能是很多事情出错的结果。检查两件事很重要:
最好的方法是更改已知有效字体文件的字体文件
如果它失败了,那么这是第一个问题
如果没有,那就是第二个,所以你必须处理FontForge
或寻找另一种字体。
答案 3 :(得分:9)
检查字体的名称和扩展名。它是区分大小写的可能全部上限。例如
Typeface tf = Typeface.createFromAsset(getAssets(), "fonts/MOLOT.OTF")
答案 4 :(得分:6)
FYI。 我崩溃的原因是由Eclipse引起的一些原因。 我所做的只是清理项目并再次运行,然后就可以了。
首先,我在我的测试项目中尝试了自定义字体,我用它来尝试一些新功能。它是第一次工作。但它在我正在进行的项目上没有奏效,直到我如上所述。
所以尝试尽可能多的方法。
答案 5 :(得分:6)
Android确实支持字体的OTF文件,如果您遇到此问题,请确保设置正确的字体路径。将字体放入资源文件夹中的文件夹字体中,并按如下所示创建字体:>
Typeface typeface = Typeface.createFromAsset(getAssets(), "font/StencilStd.otf");
TextView text = (TextView) findViewById(R.id.textView);
text.setTypeface(typeface);
答案 6 :(得分:4)
您应该使用二进制模式中的'Fontlab'软件编辑您的字体。
答案 7 :(得分:4)
@deng他的回答对我有用“:
检查字体的名称和扩展名。它是区分大小写的可能全部上限。例如
Typeface tf = Typeface.createFromAsset(getAssets(), "fonts/MOLOT.OTF")
答案 8 :(得分:1)
Android确实支持字体的OTF文件,如果您遇到此问题,请确保您为字体设置了正确的路径,例如,如果您有文件fontname.otf,则它在资源文件夹内的文件夹字体中,并创建如下字体:
Typeface typeface = Typeface.createFromAsset(getAssets(), "fonts/fontname.otf");
(路径参数不应以&#34; /&#34;开头),文件名不应包含特殊字符或&#34; - &#34;并且应该是小写