【发布时间】:2016-06-15 17:18:09
【问题描述】:
我已经阅读了所有关于在 Android 中使用字体的 SO 帖子,但我仍然无法让我的工作。我收到以下异常:
java.lang.RuntimeException: native typeface cannot be made
这是我的代码(这个类和静态方法取自here):
public class SafeTypefaces {
private static final Hashtable<String, Typeface> cache = new Hashtable<String, Typeface>();
public static Typeface get(Context c) {
final String assetPath = "fonts/robotobold.ttf";
synchronized (cache) {
if (!cache.containsKey(assetPath)) {
try {
AssetManager assetManager = c.getAssets();
Typeface t = Typeface.createFromAsset(assetManager, assetPath); //Throws exception on this line!
cache.put(assetPath, t);
} catch (Exception e) {
L.p("Could not get typeface '" + assetPath + "' because " + e.getMessage());
return null;
}
}
return cache.get(assetPath);
}
}
}
字体在assets/fonts文件夹中:
(这仍然是 Eclipse。我知道,我知道)。
我尝试过的事情:
- 我尝试将字体移动到
assets文件夹的根目录并更新assetPath。 - 我检查了文件夹和文件的权限。
- 我尝试使用应用程序上下文而不是我得到的活动上下文。
- 我尝试使用不同的字体文件(一个通过电子邮件发送,另一个从 Internet 下载)。
【问题讨论】: