11

What is happening:

  1. I have a stacktrace from the appstore as below, problem i am facing is that it dosen't show which class has caused this crash.
  2. what i can understand is that its causing due to the assets that i have used
  3. Only place i am using assets is at the application level to set the font

Code:

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

StackTrace from Appstore:

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)

What approach should i need to take to resolve this??

Devrath
  • 42,072
  • 54
  • 195
  • 297
  • Well, among other things, stop eating exceptions without logging, as you are doing in your existing code. For all you know, the information you need to fix the problem is being handed to you, and you are ignoring it. – CommonsWare May 13 '15 at 13:41
  • @ Blackbelt .... Just one ... Strange ! ... its working always!, suddenly got this crash – Devrath May 13 '15 at 13:45
  • @ CommonsWare .... Thanks for the i/p, yup i am doing a mistake by not handeling the exception. but what can i handle in case exception occurs. should i need to set default fonts or something like that ? ... any i/p – Devrath May 13 '15 at 13:46

4 Answers4

6

I saw this too from Lollipop devices with the same stacktrace, also without any reference to own code. It seems to be a platform bug coming from incomplete app updates, see https://code.google.com/p/android/issues/detail?id=56296

mattlaabs
  • 486
  • 3
  • 14
1

Try to declare the font styles separately (not in the same line), as following, change:

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");

to:

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");
sbegaudeau
  • 1,504
  • 13
  • 18
Meqdad Dev
  • 131
  • 1
  • 2
  • 10
0

Not a great workaround, but (after making sure I had the latest Android SDK updates installed) I was able to resolve this just now by uninstalling and reinstalling the app I was testing.

dstrube
  • 193
  • 1
  • 6
  • 17
-1

Add context when calling, It should be like this,-

final Typeface bold = Typeface.createFromAsset(getApplicationContext().getAssets(), "fonts/OpenSans-Bold.ttf");
Exigente05
  • 2,161
  • 3
  • 22
  • 42