由于 NPE 恢复活动时崩溃

dev*_*nlu 10 java android onresume onconfigurationchanged

我观察到我的应用程序用户发生了一些崩溃,这与配置更改和重新创建的 Activity 相关,我无法重现它。问题似乎出在 Android 类内部,因此我没有机会直接修复它。

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 + 904(Configuration.java:904)
       at android.content.res.Configuration.(Configuration.java:891)
       at android.app.ActivityThread.createNewConfigAndUpdateIfNotNull + 5133(ActivityThread.java:5133)
       at android.app.ActivityThread.performConfigurationChanged + 5203(ActivityThread.java:5203)
       at android.app.ActivityThread.performConfigurationChangedForActivity + 5117(ActivityThread.java:5117)
       at android.app.ActivityThread.handleResumeActivity + 3994(ActivityThread.java:3994)
       at android.app.ActivityThread.handleLaunchActivity + 3070(ActivityThread.java:3070)
       at android.app.ActivityThread.handleRelaunchActivity + 5006(ActivityThread.java:5006)
       at android.app.ActivityThread.-wrap21(ActivityThread.java)
       at android.app.ActivityThread$H.handleMessage + 1665(ActivityThread.java:1665)
       at android.os.Handler.dispatchMessage + 102(Handler.java:102)
       at android.os.Looper.loop + 154(Looper.java:154)
       at android.app.ActivityThread.main + 6816(ActivityThread.java:6816)
       at java.lang.reflect.Method.invoke(Method.java)
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run + 1563(ZygoteInit.java:1563)
       at com.android.internal.os.ZygoteInit.main + 1451(ZygoteInit.java:1451)
Run Code Online (Sandbox Code Playgroud)

由于我无法重新创建它,因此在 Goggle issuetracker 上打开错误报告将毫无用处。有没有人知道这里发生了什么?有没有人遇到过这个?

显然存在一个错误,因为ActivityThread#createNewConfigAndUpdateIfNotNull(Configuration, Configuration)负责将null值传递给的方法将Configuration#setTo(Configuration)其第一个参数注释为@NonNull.

崩溃主要发生在 Android 7.1.1 上,该应用程序针对 SDK 28。

小智 2

我在 android 7.1 上也遇到了这个问题,但我发现我的 Activity 创建了两次,因为在修复此问题setTheme(R.style.custom_style)后调用,super.onCreate(savedInstanceState)配置更改时不再发生崩溃

希望这有帮助