我正在尝试按照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中有什么重要的变化阻止了我这样做?
发布于 2011-05-10 12:52:08
我也得到了同样的错误,我有解决方案跟随。
Typeface tf = Typeface.createFromAsset(getAssets(), "fonts/Molot.otf")
您必须将fonts/Molot.otf
放在Eclipse的assets/fonts
文件夹中。
在你可以运行它之后。
如果你不能成功地运行它,你可以通过
发布于 2012-10-12 19:15:22
检查字体的名称和扩展名。它区分大小写&可能全部大写。例如:
Typeface tf = Typeface.createFromAsset(getAssets(), "fonts/MOLOT.OTF")
发布于 2012-01-12 17:26:29
仅供参考。我的崩溃原因是由Eclipse引起的某种原因。我所做的只是清理项目并再次运行,然后它就可以工作了。
首先,我在我的测试项目中尝试了自定义字体,我第一次使用它来尝试一些新的functions.It。但它在我正在做的项目上不起作用,直到我做了上面的事情。
因此,请尝试尽可能多的方法。
https://stackoverflow.com/questions/3023960
复制相似问题