我正在尝试在我的 android 应用程序中包含自定义字体。这是我遵循的教程,这似乎对很多人有用: http: //tharindudassanayake.wordpress.com/2012/02/25/use-sinhala-fonts-for-your-android-app/
这就是我尝试过的方式:
XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/sinhala_font"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="wdhqfndajka"
android:textSize="25sp" />
</LinearLayout>
代码:
TextView txt = (TextView) findViewById(R.id.sinhala_font);
Typeface font = Typeface.createFromAsset(getAssets(), "fonts/amal.TTF");
txt.setTypeface(font);
不幸的是应用程序崩溃(由于无法制作原生字体),我在我的日志猫中得到以下信息:
10-27 11:39:14.311: E/AndroidRuntime(28133): FATAL EXCEPTION: main
10-27 11:39:14.311: E/AndroidRuntime(28133): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.sinhala/com.example.sinhala.MainActivity}: java.lang.RuntimeException: native typeface cannot be made
10-27 11:39:14.311: E/AndroidRuntime(28133): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1970)
10-27 11:39:14.311: E/AndroidRuntime(28133): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1995)
10-27 11:39:14.311: E/AndroidRuntime(28133): at android.app.ActivityThread.access$600(ActivityThread.java:128)
10-27 11:39:14.311: E/AndroidRuntime(28133): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1161)
10-27 11:39:14.311: E/AndroidRuntime(28133): at android.os.Handler.dispatchMessage(Handler.java:99)
10-27 11:39:14.311: E/AndroidRuntime(28133): at android.os.Looper.loop(Looper.java:137)
10-27 11:39:14.311: E/AndroidRuntime(28133): at android.app.ActivityThread.main(ActivityThread.java:4517)
10-27 11:39:14.311: E/AndroidRuntime(28133): at java.lang.reflect.Method.invokeNative(Native Method)
10-27 11:39:14.311: E/AndroidRuntime(28133): at java.lang.reflect.Method.invoke(Method.java:511)
10-27 11:39:14.311: E/AndroidRuntime(28133): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:993)
10-27 11:39:14.311: E/AndroidRuntime(28133): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:760)
10-27 11:39:14.311: E/AndroidRuntime(28133): at dalvik.system.NativeStart.main(Native Method)
10-27 11:39:14.311: E/AndroidRuntime(28133): Caused by: java.lang.RuntimeException: native typeface cannot be made
10-27 11:39:14.311: E/AndroidRuntime(28133): at android.graphics.Typeface.<init>(Typeface.java:238)
10-27 11:39:14.311: E/AndroidRuntime(28133): at android.graphics.Typeface.createFromAsset(Typeface.java:212)
10-27 11:39:14.311: E/AndroidRuntime(28133): at com.example.sinhala.MainActivity.onCreate(MainActivity.java:15)
10-27 11:39:14.311: E/AndroidRuntime(28133): at android.app.Activity.performCreate(Activity.java:4470)
10-27 11:39:14.311: E/AndroidRuntime(28133): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1053)
10-27 11:39:14.311: E/AndroidRuntime(28133): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1934)
10-27 11:39:14.311: E/AndroidRuntime(28133): ... 11 more
这可能是什么原因?任何帮助表示赞赏!