尝试调用空对象引用的虚拟方法'android.content.res.AssetManager android.content.res.Resources.getAssets()'

发生什么事:

  1. 我从下面的appstore有一个堆栈跟踪,我面临的问题是它不能显示哪个类导致了这个崩溃。
  2. 我能理解的是,它是由于我用过的资产造成的
  3. 只有我使用资产的地方是在应用程序级别设置字体

码:

private void setDefaultFont() { try { final Typeface bold = Typeface.createFromAsset(getAssets(), "fonts/OpenSans-Bold.ttf"); final Typeface italic = Typeface.createFromAsset(getAssets(), "fonts/OpenSans-Italic.ttf"); final Typeface boldItalic = Typeface.createFromAsset(getAssets(), "fonts/OpenSans-BoldItalic.ttf"); final Typeface regular = Typeface.createFromAsset(getAssets(),"fonts/OpenSans-Regular.ttf"); Field DEFAULT = Typeface.class.getDeclaredField("DEFAULT"); DEFAULT.setAccessible(true); DEFAULT.set(null, regular); Field DEFAULT_BOLD = Typeface.class.getDeclaredField("DEFAULT_BOLD"); DEFAULT_BOLD.setAccessible(true); DEFAULT_BOLD.set(null, bold); Field sDefaults = Typeface.class.getDeclaredField("sDefaults"); sDefaults.setAccessible(true); sDefaults.set(null, new Typeface[]{ regular, bold, italic, boldItalic }); } catch (NoSuchFieldException e) { // logFontError(e); } catch (IllegalAccessException e) { // logFontError(e); } catch (Throwable e) { //cannot crash app if there is a failure with overriding the default font! // logFontError(e); } } 

来自Appstore的StackTrace:

 java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.AssetManager android.content.res.Resources.getAssets()' on a null object reference at android.app.LoadedApk.getAssets(LoadedApk.java:528) at android.app.LoadedApk.makeApplication(LoadedApk.java:584) at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4526) at android.app.ActivityThread.access$1500(ActivityThread.java:151) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1364) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5254) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 

我需要采取什么方法来解决这个问题?

Solutions Collecting From Web of "尝试调用空对象引用的虚拟方法'android.content.res.AssetManager android.content.res.Resources.getAssets()'"

我也从棒棒糖设备中看到了这一点,也没有提及自己的代码。 这似乎是来自不完整应用更新的平台错误,请参阅https://code.google.com/p/android/issues/detail?id=56296

尝试单独声明字体样式(不在同一行),如下所示,更改:

 final Typeface bold = Typeface.createFromAsset(getAssets(), "fonts/OpenSans-Bold.ttf"); final Typeface italic = Typeface.createFromAsset(getAssets(), "fonts/OpenSans-Italic.ttf"); final Typeface boldItalic = Typeface.createFromAsset(getAssets(), "fonts/OpenSans-BoldItalic.ttf"); final Typeface regular = Typeface.createFromAsset(getAssets(),"fonts/OpenSans-Regular.ttf"); 

至:

 final Typeface bold; final Typeface italic; final Typeface boldItalic; final Typeface regular; bold = Typeface.createFromAsset(getAssets(), "fonts/OpenSans-Bold.ttf"); italic = Typeface.createFromAsset(getAssets(), "fonts/OpenSans-Italic.ttf"); boldItalic = Typeface.createFromAsset(getAssets(), "fonts/OpenSans-BoldItalic.ttf"); regular = Typeface.createFromAsset(getAssets(),"fonts/OpenSans-Regular.ttf"); 

不是一个很好的解决方法,但(确保我已经安装了最新的Android SDK更新之后)我刚刚通过卸载并重新安装我正在testing的应用程序来解决这个问题。

调用时添加上下文,应该是这样的 –

 final Typeface bold = Typeface.createFromAsset(getApplicationContext().getAssets(), "fonts/OpenSans-Bold.ttf");