WebView 在 android 5.0 上抛出 InflateException

WebView throws InflateException on android 5.0

升级到 "androidx.appcompat:appcompat:1.0.2""com.google.android.material:material:1.1.0-beta01" 后,WebView 在 Android 5.0 的设备上崩溃并抛出此异常 android.view.InflateException

Caused by: android.view.InflateException: Binary XML file line #9: Error inflating class android.webkit.WebView

我该如何解决?

你的 targetSdkVersion 和 buildTools 版本呢?曾经我有一个非常相似的问题。当我将 targetSdkVersion 提高到 25 并将构建工具提高到 25.0.2 时,我开始看到这个异常。

同时尝试更新您的应用主题以继承自 Theme.MaterialComponents(或后代)。将您的 AppTheme 父项更改为 Theme.MaterialComponents.

例子

之前:

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
</style>

之后:

 <!-- Material application theme. -->
<style name="AppTheme" parent="Theme.MaterialComponents.Light.DarkActionBar">
    <!-- Customize your theme here. -->
</style>

如解释的那样, this issue is due to the this revision。它会影响 webview 版本 <50 的 Lollipop 设备。使用以下代码作为解决方案。

override fun applyOverrideConfiguration(overrideConfiguration: Configuration?) {
        if (Build.VERSION.SDK_INT in 21..25 && (resources.configuration.uiMode == AppConstants.appContext.resources.configuration.uiMode)) {
                return
        }
        super.applyOverrideConfiguration(overrideConfiguration)
}