android - Typeface.createFromAsset NullPointerException - 仅有时

标签 android assets android-inflate

在开始 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/

相关文章:

java - 如何通过创建另一个项目然后将其粘贴到该项目上来导入项目?

android - 无法将 Android 连接到 Web 服务

sencha-touch - 使用 sencha touch 和 rails 3.1

android - 使用 PreferenceFragment 在 Activity 中膨胀布局

java - 更改非 Activity 类中的 TextView 值

android - 当 xml 为 "inflated"时,幕后会发生什么?

android - 在开源应用程序中转换广告

android - 如何在 Exoplayer Android 库中为 HLS 流设置 CloudFront Cookies?

cakephp - 主题资源在 CakePHP 2.2.4 中不起作用

xcode - 在 xcode 颜色 Assets 中按十六进制代码搜索颜色