在Android应用程序更改字体的应用程序崩溃 - 本地字体不能发

问题描述:

我想包括在我的Andr​​oid应用程序的自定义字体。这是我跟着这似乎已经工作了许多人的教程:的http://tharindudassanayake.word$p$pss.com/2012/02/25/use-sinhala-fonts-for-your-android-app/

I am trying to include a customized font in my android application. This is the tutorial that I followed and this seems to have worked for many people: 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>

code:

Code:

TextView txt = (TextView) findViewById(R.id.sinhala_font);
        Typeface font = Typeface.createFromAsset(getAssets(), "fonts/amal.TTF");
        txt.setTypeface(font);

不幸的是应用程序崩溃(由于本地字体不能做的),我在我的日志猫如下:

Unfortunately app crashes (due to native typeface cannot be made) and I get the following in my log cat:

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

怎么可能是这个原因?任何帮助AP preciated!

What could possibly be the reason for this? Any help is appreciated!

您的资产创建名为字体的文件夹或直接放置在资产文件夹?如果文件夹中没有创建创建一个文件夹,把你的字体文件,还有我认为这是mistake..In你的榜样,他是不会创建任何字体文件夹,他被直接访问,但你是从字体文件夹访问。

Are you created a folder called fonts in assets or directly placed in assets folder??if folder not created create a folder and place your font file there I think that is the mistake..In your example he is not created any fonts folder he is directly accessing but you are accessing from fonts folder..

如果不是字体文件夹,然后更改此行。

If not fonts folder then change this line to.

Typeface font = Typeface.createFromAsset(getAssets(), "fonts/amal.TTF");

这样的..

like this..

Typeface font = Typeface.createFromAsset(getAssets(), "amal.TTF");