gpt4 book ai didi

android - Typeface.createFromAsset NullPointerException - 仅有时

转载 作者:行者123 更新时间:2023-11-30 03:41:40 25 4
gpt4 key购买 nike

在开始 Activity 中,我调用 FontFactory.init(getApplicationContext()); 将 Context 设置为 FontFactory 类。

我还有扩展 TextView 的类,在这个 TextView 的构造函数中有 setTypeface(FontFactory.Fonts.ROBOTO_LIGHT.getFont());。因此,在启动期间第一次需要时而不是之前从文件中加载字体。

问题是只是有时,不是每次都会出现启动错误和应用程序崩溃:

InflateException: Binary XML file line .. - error inflating class LayoutWithExtendedTextView

Caused by NullPointerException in Typeface nativeCreateFromAsset, createFRomAsset and FontFactory.loadFont(FontFactory.java:46)

第 46 行是return Typeface.createFromAsset(assetManager, fontEnum.getPath());

我的 FontFactory 类:

public final class FontFactory {

public enum Fonts {
ROBOTO_CONDENSED("fonts/Roboto-Condensed.ttf"), ROBOTO_LIGHT(
"fonts/Roboto-Light.ttf"), ROBOTO_MEDIUM(
"fonts/Roboto-Medium.ttf"), ROBOTO_REGULAR(
"fonts/Roboto-Regular.ttf");

private String path;
private Typeface loadedFont = null;

private Fonts(String path) {
this.path = path;
}

public String getPath() {
return path;
}

public void setLoadedFont(Typeface font) {
this.loadedFont = font;
}

public Typeface getFont() {
if (loadedFont == null) {
this.loadedFont = FontFactory.loadFont(this);
}
return loadedFont;
}
}

private static final String TAG = "FontFactory";
private static AssetManager assetManager;

public static void init(Context context) {
assetManager = context.getAssets();
}

private static Typeface loadFont(FontFactory.Fonts fontEnum) {
return Typeface.createFromAsset(assetManager, fontEnum.getPath());
}
}

加载 Assets 时是否有一些延迟?

谢谢。

最佳答案

metrhod createFromAsset 调用 nativeCreateFromAsset,这是 native C++ 代码。

static JNINativeMethod gTypefaceMethods[] = {
{ "nativeCreate", "(Ljava/lang/String;I)I", (void*)Typeface_create },
{ "nativeCreateFromTypeface", "(II)I", (void*)Typeface_createFromTypeface },
{ "nativeUnref", "(I)V", (void*)Typeface_unref },
{ "nativeGetStyle", "(I)I", (void*)Typeface_getStyle },
{ "nativeCreateFromAsset", "(Landroid/content/res/AssetManager;Ljava/lang/String;)I",
(void*)Typeface_createFromAsset },
{ "nativeCreateFromFile", "(Ljava/lang/String;)I",
(void*)Typeface_createFromFile }
};

深入研究代码,我发现了以下 Typeface_createFromAsset 的实现。

static SkTypeface* Typeface_createFromAsset(JNIEnv* env, jobject,
jobject jassetMgr,
jstring jpath) {

NPE_CHECK_RETURN_ZERO(env, jassetMgr);
NPE_CHECK_RETURN_ZERO(env, jpath);

AssetManager* mgr = assetManagerForJavaObject(env, jassetMgr);
if (NULL == mgr) {
return NULL;
}

AutoJavaStringToUTF8 str(env, jpath);
Asset* asset = mgr->open(str.c_str(), Asset::ACCESS_BUFFER);
if (NULL == asset) {
return NULL;
}

return SkTypeface::CreateFromStream(new AssetStream(asset, true));
}

该方法在以下两种情况下返回null:

  • 首先,提供的 Assets 管理人为空
  • 其次,它无法从文件系统创建加载 Assets 。

另外,请阅读问题 “Native typeface cannot be made” only for some people它可能提供了有关该问题的最完整信息(不值得在此处复制/粘贴)

关于android - Typeface.createFromAsset NullPointerException - 仅有时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15611031/

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