【问题标题】:Android – custom fonts crash appAndroid – 自定义字体崩溃应用
【发布时间】:2014-02-06 21:27:30
【问题描述】:

我正在尝试将 TextView 的字体更改为自定义字体。已阅读this other question 并尝试过说明as outlined here。有问题的字体是 Roboto Thin,直接从 Android Design 下载,在 ttf 中。

主活动类的代码如下:

TextView txt  = (TextView) findViewById(R.id.hometext);                  
Typeface font = Typeface.createFromAsset(getAssets(), "Roboto-Thin.ttf");       
txt.setTypeface(font);

// "hometext" is the TextView I wish to change font of.

当上面 3 行被注释时,应用程序不会崩溃,我希望这能隔离问题。

其他信息:

  • Android 4.1.2 (API 16)
  • 在 Android 4.4 上测试

编辑:

也试过“fonts/Roboto-Thin.ttf”和“assets/fonts/Roboto-Thin.ttf”,都不起作用。

01-17 22:38:29.086: D/AndroidRuntime(17774): Shutting down VM
01-17 22:38:29.086: W/dalvikvm(17774): threadid=1: thread exiting with uncaught exception (group=0x415f4ba8)
01-17 22:38:29.106: E/AndroidRuntime(17774): FATAL EXCEPTION: main
01-17 22:38:29.106: E/AndroidRuntime(17774): Process: sp.com, PID: 17774
01-17 22:38:29.106: E/AndroidRuntime(17774): java.lang.RuntimeException: Unable to start activity ComponentInfo{sp.com/sp.com.POSServices}: java.lang.RuntimeException: native typeface cannot be made
01-17 22:38:29.106: E/AndroidRuntime(17774):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
01-17 22:38:29.106: E/AndroidRuntime(17774):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
01-17 22:38:29.106: E/AndroidRuntime(17774):    at android.app.ActivityThread.access$800(ActivityThread.java:135)
01-17 22:38:29.106: E/AndroidRuntime(17774):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
01-17 22:38:29.106: E/AndroidRuntime(17774):    at android.os.Handler.dispatchMessage(Handler.java:102)
01-17 22:38:29.106: E/AndroidRuntime(17774):    at android.os.Looper.loop(Looper.java:136)
01-17 22:38:29.106: E/AndroidRuntime(17774):    at android.app.ActivityThread.main(ActivityThread.java:5017)
01-17 22:38:29.106: E/AndroidRuntime(17774):    at java.lang.reflect.Method.invokeNative(Native Method)
01-17 22:38:29.106: E/AndroidRuntime(17774):    at java.lang.reflect.Method.invoke(Method.java:515)
01-17 22:38:29.106: E/AndroidRuntime(17774):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
01-17 22:38:29.106: E/AndroidRuntime(17774):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
01-17 22:38:29.106: E/AndroidRuntime(17774):    at dalvik.system.NativeStart.main(Native Method)
01-17 22:38:29.106: E/AndroidRuntime(17774): Caused by: java.lang.RuntimeException: native typeface cannot be made
01-17 22:38:29.106: E/AndroidRuntime(17774):    at android.graphics.Typeface.<init>(Typeface.java:175)
01-17 22:38:29.106: E/AndroidRuntime(17774):    at android.graphics.Typeface.createFromAsset(Typeface.java:149)
01-17 22:38:29.106: E/AndroidRuntime(17774):    at sp.com.POSServices.onCreate(POSServices.java:18)
01-17 22:38:29.106: E/AndroidRuntime(17774):    at android.app.Activity.performCreate(Activity.java:5231)
01-17 22:38:29.106: E/AndroidRuntime(17774):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
01-17 22:38:29.106: E/AndroidRuntime(17774):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
01-17 22:38:29.106: E/AndroidRuntime(17774):    ... 11 more

【问题讨论】:

  • 你能发布你的stacktrace/logcat吗?
  • 您可能弄乱了存储字体的文件夹的位置。看看
  • @TimKranen 确定,当我找到它时会更新。
  • @bipin 是的,尝试了各种组合,包括“fonts/xx”和“assets/fonts/xx”。包含更新作为主 Q 中的编辑。

标签: android fonts crash


【解决方案1】:

好的,android 中有一个错误!这应该可行!

public class Typefaces {
    private static final String TAG = "Typefaces";

    private static final Hashtable<String, Typeface> cache = new Hashtable<String, Typeface>();

        public static Typeface get(Context c, String assetPath) {
            synchronized (cache) {
                if (!cache.containsKey(assetPath)) {
                    try {
                        Typeface t = Typeface.createFromAsset(c.getAssets(),
                                assetPath);
                        cache.put(assetPath, t);
                    } catch (Exception e) {
                        Log.e(TAG, "Could not get typeface '" + assetPath
                                + "' because " + e.getMessage());
                        return null;
                    }
                }
                return cache.get(assetPath);
            }
        }
    }

现在只需拨打Typefaces.get(this,"address of font here") 我来自here. 祝你好运!

【讨论】:

    【解决方案2】:

    看看你的字体在哪里。如果它在资产/字体中,那么字体的代码应该是:

    Typeface.createFromAsset(getAssets(), "fonts/Roboto-Thin.ttf");
    

    【讨论】:

    • 也试过了,只是再次确认-没有解决问题。是的,字体文件在 assets/fonts 中。
    • 好吧,这已经完成了一个修复。能发个日志吗?还要检查你的 textView 是否为空,布局中的 locsted textView 的 id 是什么?它存在吗?
    • 正在处理日志部分(如何插入 logcat 而不弄得一团糟?),但 id(“hometext”)已在布局文件中重新检查,并且肯定相符。
    • 在主帖上找到 logcat。
    【解决方案3】:

    您应该先搜索相关问题,因为它说无法制作TypeFace。你给出了正确的路径吗?如果是这样,也测试其他字体。

    与之前相同的问题: "Native typeface cannot be made" only for some people

    另外,请使用适用于 Android 的 Fontify 库。它让一切变得更轻松、更好!

    网址: https://github.com/danh32/Fontify

    【讨论】:

    • 是的,实际上我特别小心地进行了广泛的搜索(因为我的关键字可以带我 - 由于缺乏 Android 开发经验),因为我为类似/精确的问题设计了我的问题Q's,但由于选择了标题中的单词(我想),上述链接没有出现。此外,不熟悉阅读 logcats .. 对此表示歉意。谢谢你所做的一切,真的帮助了我作为一个初学者。
    • 不客气!让我知道您是否可以修复它,否则,如果您愿意,我可以为您编写示例源代码。给我发送一封包含您的应用程序的电子邮件,我可以为您修复它。我的电子邮件是 Xarialon@gmail.com
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多