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