In my starting activity, I call FontFactory.init(getApplicationContext());
to set Context to FontFactory class.
I have also class which extends TextView
and in constructor of this TextView there is setTypeface(FontFactory.Fonts.ROBOTO_LIGHT.getFont());
. So a font from file is loaded when first needed, not before, during startup.
Problem is that only sometimes, not every time there is a startup error and application crashes:
InflateException: Binary XML file line .. - error inflating class LayoutWithExtendedTextView
Caused by NullPointerException in Typeface nativeCreateFromAsset, createFRomAsset and FontFactory.loadFont(FontFactory.java:46)
Line 46 is return Typeface.createFromAsset(assetManager, fontEnum.getPath());
My FontFactory class:
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());
}
}
Is there some delay when loading asset?
Thank you.