gpt4 book ai didi

android - "Native typeface cannot be made"仅适用于某些人

转载 作者:IT老高 更新时间:2023-10-28 13:11:57 25 4
gpt4 key购买 nike

我有一个应用程序可以更改某些元素的字体。它适用于大多数人,但可能有 0.5% 的人在尝试更改字体时会出现异常。堆栈跟踪的重要部分是:

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)

正如我所说,它适用于大多数人,所以我认为字体文件或我的代码没有问题。有关如何解决此问题的任何建议?

编辑:这是我的代码:

Typeface phoneticFont = Typeface.createFromAsset(getAssets(),
"fonts/CharisSILR.ttf");
TextView tv;
tv = ((TextView) findViewById(R.id.searchPronunciationTitle));
tv.setTypeface(phoneticFont);

最佳答案

Android 操作系统的这个错误可能是您的问题的原因:

Typeface.createFromAsset leaks asset stream

此错误报告中还有哪些解决方法:

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);
}
}
}

关于android - "Native typeface cannot be made"仅适用于某些人,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12766930/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com