Android 报错:InflateException: Binary XML file line #10: Error inflating class android.webkit.WebView

文章目录

  • 问题描述
  • 解决方案

问题描述

只是在 layout 中引入了 WebView ,然后在 Activity 中写了setContentView(),在 Android 5.1 的机子运行时就出现了错误。
代码如下:
activity_main.xml


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".webviewtest.Main18Activity">

    <WebView
        android:id="@+id/web_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
LinearLayout>

MainActivity.java

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import com.example.myapplication.R;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}

报错如下:

com.example.myapplication E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.myapplication, PID: 23923
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.myapplication/com.example.myapplication.webviewtest.Main18Activity}: android.view.InflateException: Binary XML file line #10: Error inflating class android.webkit.WebView
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2510)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2579)
        at android.app.ActivityThread.access$800(ActivityThread.java:176)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1452)
        at android.os.Handler.dispatchMessage(Handler.java:111)
        at android.os.Looper.loop(Looper.java:192)
        at android.app.ActivityThread.main(ActivityThread.java:5618)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:976)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:771)
     Caused by: android.view.InflateException: Binary XML file line #10: Error inflating class android.webkit.WebView
        at android.view.LayoutInflater.createView(LayoutInflater.java:633)
        at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:55)
        at android.view.LayoutInflater.onCreateView(LayoutInflater.java:682)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:741)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
        at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:555)
        at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:161)
        at com.example.myapplication.webviewtest.Main18Activity.onCreate(Main18Activity.java:16)
        at android.app.Activity.performCreate(Activity.java:6135)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1122)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2463)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2579) 
        at android.app.ActivityThread.access$800(ActivityThread.java:176) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1452) 
        at android.os.Handler.dispatchMessage(Handler.java:111) 
        at android.os.Looper.loop(Looper.java:192) 
        at android.app.ActivityThread.main(ActivityThread.java:5618) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at java.lang.reflect.Method.invoke(Method.java:372) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:976) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:771) 
     Caused by: java.lang.reflect.InvocationTargetException
        at java.lang.reflect.Constructor.newInstance(Native Method)
        at java.lang.reflect.Constructor.newInstance(Constructor.java:288)
        at android.view.LayoutInflater.createView(LayoutInflater.java:607)
        at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:55) 
        at android.view.LayoutInflater.onCreateView(LayoutInflater.java:682) 
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:741) 
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:806) 
        at android.view.LayoutInflater.inflate(LayoutInflater.java:504) 
        at android.view.LayoutInflater.inflate(LayoutInflater.java:414) 
        at android.view.LayoutInflater.inflate(LayoutInflater.java:365) 
        at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:555) 
        at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:161) 
        at com.example.myapplication.webviewtest.Main18Activity.onCreate(Main18Activity.java:16) 
        at android.app.Activity.performCreate(Activity.java:6135) 
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1122) 
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2463) 
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2579) 
        at android.app.ActivityThread.access$800(ActivityThread.java:176) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1452) 
        at android.os.Handler.dispatchMessage(Handler.java:111) 
        at android.os.Looper.loop(Looper.java:192) 
        at android.app.ActivityThread.main(ActivityThread.java:5618) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at java.lang.reflect.Method.invoke(Method.java:372) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:976) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:771) 
     Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x2040003
        at android.content.res.Resources.getText(Resources.java:322)
        at android.content.res.Resources.getString(Resources.java:420)
        at com.android.org.chromium.content.browser.ContentViewCore.setContainerView(ContentViewCore.java:684)
        at com.android.org.chromium.content.browser.ContentViewCore.initialize(ContentViewCore.java:608)
        at com.android.org.chromium.android_webview.AwContents.createAndInitializeContentViewCore(AwContents.java:674)
        at com.android.org.chromium.android_webview.AwContents.setNewAwContents(AwContents.java:823)
        at com.android.org.chromium.android_webview.AwContents.(AwContents.java:662)
        at com.android.org.chromium.android_webview.AwContents.(AwContents.java:597)
        at com.android.webview.chromium.WebViewChromium.initForReal(WebViewChromium.java:319)
        at com.android.webview.chromium.WebViewChromium.access$100(WebViewChromium.java:104)
        at com.android.webview.chromium.WebViewChromium$1.run(WebViewChromium.java:271)
        at com.android.webview.chromium.WebViewChromium$WebViewChromiumRunQueue.drainQueue(WebViewChromium.java:131)
        at com.android.webview.chromium.WebViewChromium$WebViewChromiumRunQueue$1.run(WebViewChromium.java:118)
        at com.android.org.chromium.base.ThreadUtils.runOnUiThread(ThreadUtils.java:144)
        at com.android.webview.chromium.WebViewChromium$WebViewChromiumRunQueue.addTask(WebViewChromium.java:115)
        at com.android.webview.chromium.WebViewChromium.init(WebViewChromium.java:268)
    	at android.webkit.WebView.(WebView

解决方案

将 app 目录下的 build.gradle 文件里的
implementation 'androidx.appcompat:appcompat:1.1.0'
修改为
implementation 'androidx.appcompat:appcompat:1.0.2'
经测试,修改为 版本 1.2.0-alpha01 仍然会出现错误。

你可能感兴趣的:(Android)