前言
在佈局文件中直接使用 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