【问题标题】:Native Typeface Cannot be Made for 4.0.3-4.0.4不能为 4.0.3-4.0.4 制作原生字体
【发布时间】:2014-05-12 11:31:35
【问题描述】:

我刚刚在应用商店中启动了一个应用,并收到人们关于夸大我的自定义 TextView 的错误,我用它来实现自定义字体。这些错误似乎来自运行 4.0.3 或 4.0.4 的人,它在运行 4.3 的 S3 和 Nexus 7 上运行良好。我是否缺少一些东西来使其兼容?我知道this SO question 有同样的问题,但并没有完全解决。我添加了一个 try/catch 让它默认恢复为内置字体,但似乎应该有一个解决方案,所以我可以使用我的自定义字体。

Logcat:

Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Constructor.constructNative(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
at android.view.LayoutInflater.createView(LayoutInflater.java:586)
... 22 more
Caused by: java.lang.RuntimeException: native typeface cannot be made
at android.graphics.Typeface.<init>(Typeface.java:430)
at android.graphics.Typeface.createFromAsset(Typeface.java:404)
at com.gtf.oneparish.AvTextView.<init>(AvTextView.java:18)
... 25 more

AvTextView:

public class AvTextView extends TextView {
    public static Typeface FONT_NAME;


    public AvTextView(Context context) {
        super(context);
        try {
            if(FONT_NAME == null) FONT_NAME = Typeface.createFromAsset(context.getAssets(), "fonts/avenirlight.ttf");
            this.setTypeface(FONT_NAME);
        } catch (Exception e) {
            Log.e("Font Creation error", "Could not get typeface because " + e.getMessage());
        }
    }
    public AvTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        try {
            if(FONT_NAME == null) FONT_NAME = Typeface.createFromAsset(context.getAssets(), "fonts/avenirlight.ttf");
            this.setTypeface(FONT_NAME);
        } catch (Exception e) {
            Log.e("Font Creation error", "Could not get typeface because " + e.getMessage());
        }
    }
    public AvTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        try {
            if(FONT_NAME == null) FONT_NAME = Typeface.createFromAsset(context.getAssets(), "fonts/avenirlight.ttf");
            this.setTypeface(FONT_NAME);
        } catch (Exception e) {
            Log.e("Font Creation error", "Could not get typeface because " + e.getMessage());
        }
    }
}

【问题讨论】:

    标签: android


    【解决方案1】:

    Android 4.0.x 似乎对字体有点挑剔,无论出于何种原因都拒绝加载其中的一些字体。我能够通过在 FontForge 中调整其字体信息(以更紧密地匹配另一种成功加载的字体)并生成一个新的 .ttf 文件来加载我的自定义字体。原始 .ttf 在我的计算机和 Android 2.3.x、4.3 和 4.4 上运行良好,但在 4.0.3 和 4.0.4 中引发异常。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-10-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-23
      相关资源
      最近更新 更多