"本地字体不能作出"只对某些人 [英] "Native typeface cannot be made" only for some people
问题描述
我有一个应用程序,更改字体字样的某些元素。它适用于大多数人,但也许尝试更改字体时,0.5%,得到一个异常。堆栈跟踪的显著的部分是这样的:
产生的原因:java.lang.RuntimeException的:本地字体不可言
在android.graphics.Typeface< INIT>(Typeface.java:147)
在android.graphics.Typeface.createFromAsset(Typeface.java:121)
正如我说的,它适用于大多数人,所以我不认为这是与字体文件或我的code的一个问题。关于如何解决这个任何建议?
编辑:这是我的code:
字样phoneticFont = Typeface.createFromAsset(getAssets()
字体/ CharisSILR.ttf);
TextView的电视;
电视=((TextView中)findViewById(R.id.searchPronunciationTitle));
tv.setTypeface(phoneticFont);
Android操作系统的这种错误可能是你的问题的原因:
还都在这个错误报告要解决此问题其中:
我改变了心连心的解决方法,使该方法不承担字体 路径或格式。字体资产的完整路径必须提交 的参数。我还裹着调用createFromAsset()在 try-catch块,这样的get()方法,如果资产返回null 未找到。
公共类字样{
私有静态最后字符串变量=字样;
私有静态最后的Hashtable<字符串,字体>缓存=新的Hashtable<字符串,字体>();
公共静态字样的get(上下文C,串assetPath){
同步(缓存){
如果(!cache.containsKey(assetPath)){
尝试 {
字样T = Typeface.createFromAsset(c.getAssets()
assetPath);
cache.put(assetPath,T);
}赶上(例外五){
Log.e(TAG,无法获取字体'+ assetPath
+,因为+ e.getMessage());
返回null;
}
}
返回cache.get(assetPath);
}
}
}
I have an app that changes the font typeface for some elements. It works well for most of the people, but maybe a 0.5% get an exception when trying to change the font. The significant part of the stack trace is this:
Caused by: java.lang.RuntimeException: native typeface cannot be made
at android.graphics.Typeface.<init>(Typeface.java:147)
at android.graphics.Typeface.createFromAsset(Typeface.java:121)
As I say, it works for most of the people, so I don't think it is a problem with the font file or my code. Any suggestions about how to solve this?
Edit: This is my code:
Typeface phoneticFont = Typeface.createFromAsset(getAssets(),
"fonts/CharisSILR.ttf");
TextView tv;
tv = ((TextView) findViewById(R.id.searchPronunciationTitle));
tv.setTypeface(phoneticFont);
This bug of Android OS could be the reason of your issue:
Typeface.createFromAsset leaks asset stream
Where are also a workaround in this bugreport:
I altered HTH's workaround so that the method does not assume the font path or format. The full path of the font asset must be submitted as a parameter. I also wrapped the call to createFromAsset() in a try-catch block so that the get() method will return null if the asset is not found.
public class Typefaces {
private static final String TAG = "Typefaces";
private static final Hashtable<String, Typeface> cache = new Hashtable<String, Typeface>();
public static Typeface get(Context c, String assetPath) {
synchronized (cache) {
if (!cache.containsKey(assetPath)) {
try {
Typeface t = Typeface.createFromAsset(c.getAssets(),
assetPath);
cache.put(assetPath, t);
} catch (Exception e) {
Log.e(TAG, "Could not get typeface '" + assetPath
+ "' because " + e.getMessage());
return null;
}
}
return cache.get(assetPath);
}
}
}
这篇关于&QUOT;本地字体不能作出&QUOT;只对某些人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!