6

I am struggling to solve this error that happens only when I am on night mode (I am using Theme.MaterialComponents.DayNight) and when rotating the screen... on day mode it works fine with no issues.

Attempt to read from field 'float android.content.res.Configuration.fontScale' on a null object reference

I have no idea where it is coming from, I am not using any custom fonts.

jkdev
  • 11,360
  • 15
  • 54
  • 77
joghm
  • 569
  • 3
  • 20
  • That's not at all a nullpointerexception typical question, i had no control over the use of that object, neither the fonscale.. the app fully function as it needs to be on android version less than 7.0 but crashes on 7 and above. i am doubting it might be a bug within the Theme.MaterialComponents.DayNight there. – joghm Nov 26 '19 at 14:46
  • 2
    This is not a duplicate question. – Rachit Mishra Dec 03 '19 at 19:34

3 Answers3

4

I have experienced the same issue when I call AppCompatDelegate.setDefaultNightMode from the main activity's onCreate.

My workaround is simple: call it from a fragment's onCreateView. I can't explain why this works but it just works for me.

(Updated 2/4/20) Please see Jason Marks' answer, which works better than mine.

Yihong WU
  • 51
  • 5
2

Try calling AppCompatDelegate.setDefaultNightMode() before super.onCreate(...) in your Activity

0

I am facing the same issue on a lot of Android 7.0 devices, most of our users use the app in 'Landscape' mode & they use 'Night Mode'.

I wasn't getting this exception when versions were this:

implementation "android.arch.lifecycle:viewmodel:1.1.1"
implementation 'android.arch.lifecycle:runtime:1.1.1'
implementation 'android.arch.lifecycle:extensions:1.1.1'
annotationProcessor "android.arch.lifecycle:compiler:1.1.1"
implementation "android.arch.lifecycle:common-java8:1.1.1"

But when I updated these libraries, I started getting this error.

def lifecycle_version = "2.1.0"
implementation "androidx.lifecycle:lifecycle-viewmodel:$lifecycle_version"
implementation "androidx.lifecycle:lifecycle-runtime:$lifecycle_version"
implementation "androidx.lifecycle:lifecycle-extensions:$lifecycle_version"
kapt "androidx.lifecycle:lifecycle-common-java8:$lifecycle_version"

Fatal Exception: java.lang.NullPointerException: Attempt to read from field 'float android.content.res.Configuration.fontScale' on a null object reference
       at android.content.res.Configuration.setTo + 752(Configuration.java:752)
       at android.content.res.Configuration.<init> + 739(Configuration.java:739)
       at android.app.ActivityThread.createNewConfigAndUpdateIfNotNull + 4731(ActivityThread.java:4731)
       at android.app.ActivityThread.performConfigurationChanged + 4793(ActivityThread.java:4793)
       at android.app.ActivityThread.performConfigurationChangedForActivity + 4715(ActivityThread.java:4715)
       at android.app.ActivityThread.handleConfigurationChanged + 4891(ActivityThread.java:4891)
       at android.app.ActivityThread$H.handleMessage + 1644(ActivityThread.java:1644)
       at android.os.Handler.dispatchMessage + 110(Handler.java:110)
       at android.os.Looper.loop + 203(Looper.java:203)
       at android.app.ActivityThread.main + 6251(ActivityThread.java:6251)
       at java.lang.reflect.Method.invoke(Method.java)
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run + 1063(ZygoteInit.java:1063)
       at com.android.internal.os.ZygoteInit.main + 924(ZygoteInit.java:924)
Umair Adil
  • 968
  • 1
  • 10
  • 24