前言
在布局文件中直接使用 WebView ,Activity 运行时,APP崩溃
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/appBg"
android:orientation="vertical">
<WebView
android:id="@+id/webView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
关键异常
1 Caused by: android.view.InflateException: Binary XML file line #8: Error inflating class android.webkit.WebView
2 Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x2040003
异常问题分析
简要概述为:
系统 WebView 出现了问题
一般是特殊机型或刷机后导致系统WebView被卸载或文件缺失的情况
也有者为系统WebView为64位的,并不支持
推荐以下两篇博客,写的很详细
android.view.InflateException Error inflating class android.webkit.WebView
解决方案汇总
方案一
在 app 下的 build.gradle 配置文件中,将 androidx.appcompat:appcompat 的版本改为 1.0.2
implementation 'androidx.appcompat:appcompat:1.0.2'
方案二
在 AndroidManifest.xml 文件的 application 标签下增加如下配置
<meta-data
android:name="android.webkit.WebView.EnableSafeBrowsing"
android:value="true" />
方案三
使用第三方的浏览器内核
如腾讯提供的浏览器内核SDK