【问题标题】:Changing font in android app crashes the application - native typeface cannot be made在 android 应用程序中更改字体会使应用程序崩溃 - 无法制作本机字体
【发布时间】:2013-11-06 01:40:15
【问题描述】:

我正在尝试在我的 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

这可能是什么原因?任何帮助表示赞赏!

【问题讨论】:

  • 这里有一个为整个应用程序更改字体的有效方法:stackoverflow.com/questions/18847531/…
  • 我厌倦了这个答案,它抛出了一个异常:无法制作原生字体。需要做什么?

标签: android fonts android-typeface


【解决方案1】:

您是在资产中创建了一个名为fonts 的文件夹还是直接放置在assets 文件夹中?没有创建他直接访问的任何fonts 文件夹,但您正在从字体文件夹访问..

如果不是字体文件夹,则将此行更改为。

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

像这样..

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

【讨论】:

    【解决方案2】:

    我得到了同样的例外。 就我而言,我给我的 ttf 文件提供了错误的路径。 我写道:/font/somefont.ttf 我通过删除第一个连字符来修复它:font/somefont.ttf

    【讨论】:

      【解决方案3】:

      如果您使用的是 Android Studio,则可以省去很多麻烦,只需将“assets/”文件夹放在“src/main”中,而不是与“src/”处于同一级别。

      【讨论】:

      • 谢谢,这个解决方案对我有用! ${project_root}\app\src\main\assets\font.ttf
      【解决方案4】:

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

      Typeface font = Typeface.createFromAsset(getAssets(), "fonts/amal.ttf"); 这为我解决了。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-08-02
        • 2015-12-19
        • 1970-01-01
        • 2015-10-22
        • 1970-01-01
        • 2014-01-16
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多