android - 在 Android 应用程序中更改字体会使应用程序崩溃 - 无法制作 native 字体

标签 android fonts android-typeface

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

这可能是什么原因?感谢您的帮助!

最佳答案

您是在 Assets 中创建了一个名为 fonts 的文件夹,还是直接放置在 assets 文件夹中?如果没有创建文件夹,请创建一个文件夹并将您的字体文件放在那里我认为是错误的..在你的例子中,他没有创建他直接访问的任何 fonts 文件夹,但你正在从 fonts 文件夹访问..

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

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

像这样..

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

关于android - 在 Android 应用程序中更改字体会使应用程序崩溃 - 无法制作 native 字体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19615217/

相关文章:

Android 版本和字体支持(sans-serif/Roboto)?

android - Font-awesome 显示一些奇怪的图标

java - 如何从 Google Play 服务库仅导入一个包

android - 当设备是 iPhone 时如何显示后退按钮

java - 列出 c2dm/GCM 注册应用程序

html - 文本区域中的字体一致性

javascript - 以编程方式访问 JavaScript 中的字体规范

android - 设置回退字体 android 应用程序

android - 监听WIFI状态

html - 我的字体在 chrome 浏览器中不同