简体   繁体   English

由于 NPE 恢复活动时崩溃

[英]Crash when resuming Activity due to NPE

I've observed some crashes among the users of my app which is related to a configuration change and the Activity being recreated, I was not able to reproduce it.我观察到我的应用程序用户发生了一些崩溃,这与配置更改和重新创建的 Activity 相关,我无法重现它。 The issue seems to be inside the Android classes and therefore there's no chance for me to fix it directly.问题似乎出在 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)

Since I'm not able to recreate it, opening a bug report on Goggle issuetracker would be pretty useless.由于我无法重新创建它,因此在 Goggle issuetracker 上打开错误报告将毫无用处。 Does anyone have a clue on what's happening here?有没有人知道这里发生了什么? Has anyone encountered this before?有没有人遇到过这个?

There is clearly a bug because the ActivityThread#createNewConfigAndUpdateIfNotNull(Configuration, Configuration) method, which is responsible for passing a null value to Configuration#setTo(Configuration) has its first parameter annotated as @NonNull .显然存在一个错误,因为负责将null值传递给Configuration#setTo(Configuration)ActivityThread#createNewConfigAndUpdateIfNotNull(Configuration, Configuration)方法将其第一个参数注释为@NonNull

The crashed happened mainly on Android 7.1.1, the app targets SDK 28.崩溃主要发生在 Android 7.1.1 上,该应用程序针对 SDK 28。

此问题仅在 android 7.0 及更高版本中发生...当应用程序在夜间模式下创建并且屏幕旋转(配置更改)时,这可能是 DayNight 主题中的错误。

I have this issue too on android 7.1 but i find out my activity created twice because setTheme(R.style.custom_style) called after super.onCreate(savedInstanceState) when fix this issue crash not happened anymore on configuration changes我在 android 7.1 上也有这个问题,但我发现我的活动创建了两次,因为setTheme(R.style.custom_style)super.onCreate(savedInstanceState)之后super.onCreate(savedInstanceState)修复这个问题崩溃不再发生在配置更改

hope this help希望这有帮助

I did a lot of research on this problem and I noticed that this problem only occurs in Android 7 and in the DayNight theme.我对这个问题做了很多研究,我注意到这个问题只出现在 Android 7 和 DayNight 主题中。 To solve this problem, you can not use the DayNight theme in Android 7, such as the following code:为了解决这个问题,Android 7 中不能使用 DayNight 主题,如以下代码:

 if (Build.VERSION.SDK_INT==Build.VERSION_CODES.N||Build.VERSION.SDK_INT==Build.VERSION_CODES.N_MR1){
            AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
        }

My problem was solved by adding the above code I hope this solution has been helpful.通过添加上面的代码解决了我的问题我希望这个解决方案有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM