android - Typeface.createFromAsset NullPointerException - only sometimes -


in starting activity, call fontfactory.init(getapplicationcontext()); set context fontfactory class.

i have class extends textview , in constructor of textview there settypeface(fontfactory.fonts.roboto_light.getfont());. font file loaded when first needed, not before, during startup.

problem only sometimes, not every time there startup error , application crashes:

inflateexception: binary xml file line .. - error inflating class layoutwithextendedtextview

caused nullpointerexception in typeface nativecreatefromasset, createfromasset , fontfactory.loadfont(fontfactory.java:46)

line 46 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 delay when loading asset?

thank you.

metrhod createfromasset calls nativecreatefromasset native c++ code.

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

digging through code, found following implementation of 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)); } 

this method returns null in following 2 circumstances:

  • first, asset manager provided null
  • second, unable create load asset file system.

also, read question “native typeface cannot made” people provides complete information regarding issue (not worth copy/pasting here)


Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

How to get the ip address of VM and use it to configure SSH connection dynamically in Ansible -

javascript - Get parameter of GET request -