【发布时间】:2014-10-25 07:10:06
【问题描述】:
我在我的 Android 应用程序中为 TextView 和 EditText 使用自定义字体。我为字体编辑文本和文本视图创建了一个单独的类。我正在为我的文本使用 Calibri 字体。
但是在运行我的应用程序后,它在所有文件中都显示以下错误 -
com.pack.demo.MyFontedTextView 实例化失败,其中 MyFontedTextView 是类文件,com.pack.demo.MyFontedEditText 实例化失败。
在我所有的布局中,运行时异常是 -
java.lang.RuntimeException: native typeface cannot be made
at android.graphics.Typeface.<init>(Typeface.java:175)
at android.graphics.Typeface.createFromAsset(Typeface.java:149)
at com.pack.demo.MyFontedTextView.<init>(MyFontedTextView.java:22)
com.pack.demo.MyFontedTextView.(MyFontedTextView.java:22) 显示字体错误 font = Typeface.createFromAsset(context.getAssets(), "calibri.otf");在下面的类文件中。
这是我的字体编辑文本的代码 -
public class MyFontedEditText extends EditText {
public MyFontedEditText(Context context) {
super(context);
// TODO Auto-generated constructor stub
Typeface font = Typeface.createFromAsset(context.getAssets(),
"calibri.otf");
this.setTypeface(font);
}
public MyFontedEditText(Context context, AttributeSet attrs) {
super(context, attrs);
Typeface font = Typeface.createFromAsset(context.getAssets(),
"calibri.otf");
this.setTypeface(font);
}
public MyFontedEditText(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
Typeface font = Typeface.createFromAsset(context.getAssets(),
"calibri.otf");
this.setTypeface(font);
}
}
同样,我的字体文本视图代码 -
public class MyFontedTextView extends TextView {
public MyFontedTextView(Context context) {
super(context);
// TODO Auto-generated constructor stub
Typeface font = Typeface.createFromAsset(context.getAssets(),
"calibri.otf");
this.setTypeface(font);
this.setTypeface(null, Typeface.BOLD);
}
public MyFontedTextView(Context context, AttributeSet attrs) {
super(context, attrs);
Typeface font = Typeface.createFromAsset(context.getAssets(),
"calibri.otf");
Log.d("1", "abc");
this.setTypeface(font);
this.setTypeface(null, Typeface.BOLD);
}
public MyFontedTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
Typeface font = Typeface.createFromAsset(context.getAssets(),
"calibri.otf");
this.setTypeface(font);
this.setTypeface(null, Typeface.BOLD);
}
}
【问题讨论】:
-
你的字体添加到 assets 文件夹了吗?
-
@Sishin..是的,我已经添加了,但仍然无法正常工作..
-
我认为是拼写错误。将您的 .ttf 文件名检查为您在课堂上提到的名称。
-
希望你的资产中没有字体文件夹?
-
@Ashutosh 我只是想告诉你检查你的字体没有损坏
标签: android android-layout fonts