Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error on night mode when rotating the screen

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.

like image 453
joghm Avatar asked Nov 25 '19 17:11

joghm


People also ask

How do you reset night mode?

To turn off the Dark Mode in Windows 10, open Settings and go to Personalization. On the left column, choose Colors, and then select the following options: In the "Choose your color" dropdown list, choose Custom. Under "Choose your default Windows mode," select Dark.

How do I fix Dark Mode on my Android?

On your phone, open the Settings app. Tap Display. Turn Dark theme on or off.

How do you disable night mode in my application even if night mode is enable in Android 9.0 PIE )?

Disable all over the app: AppCompatDelegate. setDefaultNightMode(AppCompatDelegate. MODE_NIGHT_NO)

Why does my Samsung keep switching from night mode?

Since Nov. 21, 2019 some Samsung Galaxy devices repeatedly switch back to day mode. This not an issue with the Dark Mode app but with the Email app by Samsung. The Samsung Email app overwrites the Android night mode settings which may switch your device back to day mode.


2 Answers

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.

like image 141
Yihong WU Avatar answered Oct 20 '22 04:10

Yihong WU


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

like image 30
Jason Marks Avatar answered Oct 20 '22 04:10

Jason Marks