【发布时间】: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