我的代码存在一个严重问题,我正在从自定义的TextView类中加载位于“assets\fonts\”文件夹中的字体。第一个问题是,在4.0设备上会崩溃,并显示异常信息“Caused by: java.lang.RuntimeException: native typeface cannot be made”。我的使用方法与此处相同。
注意到我正在使用扩展名为
public class MyTextView extends TextView {
public MyTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public MyTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyTextView(Context context) {
super(context);
}
public void setTypeface(Typeface tf, int style) {
if (style == Typeface.BOLD) {
super.setTypeface(Typeface.createFromAsset(
getContext().getAssets(), "fonts/hirakakupronbold.ttf"));
} else if (style == Typeface.ITALIC) {
super.setTypeface(Typeface.createFromAsset(
getContext().getAssets(), "fonts/hirakakupronitalic.ttf"));
} else {
super.setTypeface(Typeface.createFromAsset(
getContext().getAssets(), "fonts/hirakakupron.ttf"));
}
}
}
注意到我正在使用扩展名为
.ttf
的字体文件,发现这导致了RunTimeException
。因此,我将相应的字体转换成了.otf
格式,现在它可以在4.0设备上运行,但存在内存泄漏问题,见这里。有一些解决方法在这里,但我不知道如何使用或调用它们。希望能得到帮助,谢谢。