WebView 中的“android.view.InflateException: Binary XML file Error inflating class”错误

Posted

技术标签:

【中文标题】WebView 中的“android.view.InflateException: Binary XML file Error inflating class”错误【英文标题】:"android.view.InflateException: Binary XML file Error inflating class" error in WebView 【发布时间】:2016-07-27 08:30:40 【问题描述】:

我的自定义 WebView 类出现错误: java.lang.RuntimeException: Unable to start activity ComponentInfocom.example.ates.aaa/com.example.ates.aaa.MainActivity: android.view.InflateException: Binary XML file line #9: Error inflating class com.example.ates.aaa.MyWebView

我的网络视图:

public class MyWebView extends WebView 

    public MyWebView(Context context) 
        super(context);
    


主活动:

MyWebView wv;

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

    wv = (MyWebView) findViewById(R.id.myWebView);
    wv.loadUrl("www.google.com");


activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_
    android:layout_
    android:paddingLeft="16dp"
    android:paddingRight="16dp" >


    <com.example.ates.aaa.MyWebView
        android:layout_
        android:layout_
        android:id="@+id/myWebView" />


</RelativeLayout>

完整的堆栈跟踪:

04-06 11:50:46.267 8818-8818/com.example.ates.aaa D/dalvikvm: Late-enabling CheckJNI
04-06 11:50:46.283 8818-8818/com.example.ates.aaa W/dalvikvm: VFY: unable to find class referenced in signature (Landroid/view/SearchEvent;)
04-06 11:50:46.283 8818-8818/com.example.ates.aaa I/dalvikvm: Could not find method android.view.Window$Callback.onSearchRequested, referenced from method android.support.v7.view.WindowCallbackWrapper.onSearchRequested
04-06 11:50:46.283 8818-8818/com.example.ates.aaa W/dalvikvm: VFY: unable to resolve interface method 18801: Landroid/view/Window$Callback;.onSearchRequested (Landroid/view/SearchEvent;)Z
04-06 11:50:46.283 8818-8818/com.example.ates.aaa D/dalvikvm: VFY: replacing opcode 0x72 at 0x0002
04-06 11:50:46.283 8818-8818/com.example.ates.aaa I/dalvikvm: Could not find method android.view.Window$Callback.onWindowStartingActionMode, referenced from method android.support.v7.view.WindowCallbackWrapper.onWindowStartingActionMode
04-06 11:50:46.283 8818-8818/com.example.ates.aaa W/dalvikvm: VFY: unable to resolve interface method 18805: Landroid/view/Window$Callback;.onWindowStartingActionMode (Landroid/view/ActionMode$Callback;I)Landroid/view/ActionMode;
04-06 11:50:46.283 8818-8818/com.example.ates.aaa D/dalvikvm: VFY: replacing opcode 0x72 at 0x0002
04-06 11:50:46.303 8818-8818/com.example.ates.aaa D/AndroidRuntime: Shutting down VM
04-06 11:50:46.303 8818-8818/com.example.ates.aaa W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0xa62f4288)
04-06 11:50:46.331 8818-8818/com.example.ates.aaa E/AndroidRuntime: 
    FATAL EXCEPTION: main
    java.lang.RuntimeException: Unable to start activity ComponentInfocom.example.ates.aaa/com.example.ates.aaa.MainActivity: android.view.InflateException: Binary XML file line #9: Error inflating class com.example.ates.aaa.MyWebView
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
        at android.app.ActivityThread.access$600(ActivityThread.java:130)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:4745)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
        at dalvik.system.NativeStart.main(Native Method)
    Caused by: android.view.InflateException: Binary XML file line #9: Error inflating class com.example.ates.aaa.MyWebView
        at android.view.LayoutInflater.createView(LayoutInflater.java:596)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:687)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
        at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:267)
        at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:129)
        at com.example.ates.aaa.MainActivity.onCreate(MainActivity.java:20)
        at android.app.Activity.performCreate(Activity.java:5008)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084) 
        at android.app.ActivityThread.access$600(ActivityThread.java:130) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195) 
        at android.os.Handler.dispatchMessage(Handler.java:99) 
        at android.os.Looper.loop(Looper.java:137) 
        at android.app.ActivityThread.main(ActivityThread.java:4745) 
        at java.lang.reflect.Method.invokeNative(Native Method) 
        at java.lang.reflect.Method.invoke(Method.java:511) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 
        at dalvik.system.NativeStart.main(Native Method) 
    Caused by: java.lang.NoSuchMethodException: <init> [class android.content.Context, interface android.util.AttributeSet]
        at java.lang.Class.getConstructorOrMethod(Class.java:460)
        at java.lang.Class.getConstructor(Class.java:431)
        at android.view.LayoutInflater.createView(LayoutInflater.java:561)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:687) 
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:746) 
        at android.view.LayoutInflater.inflate(LayoutInflater.java:489) 
        at android.view.LayoutInflater.inflate(LayoutInflater.java:396) 
        at android.view.LayoutInflater.inflate(LayoutInflater.java:352) 
        at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:267) 
        at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:129) 
        at com.example.ates.aaa.MainActivity.onCreate(MainActivity.java:20) 
        at android.app.Activity.performCreate(Activity.java:5008) 
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079) 
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023) 
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084) 
        at android.app.ActivityThread.access$600(ActivityThread.java:130) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195) 
        at android.os.Handler.dispatchMessage(Handler.java:99) 
        at android.os.Looper.loop(Looper.java:137) 
        at android.app.ActivityThread.main(ActivityThread.java:4745) 
        at java.lang.reflect.Method.invokeNative(Native Method) 
        at java.lang.reflect.Method.invoke(Method.java:511) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 
        at dalvik.system.NativeStart.main(Native Method) 
04-06 11:50:46.351 8818-8821/com.example.ates.aaa D/dalvikvm: GC_CONCURRENT freed 229K, 3% free 10898K/11207K, paused 10ms+0ms, total 12ms
04-06 11:55:46.391 8818-8818/? I/Process: Sending signal. PID: 8818 SIG: 9

【问题讨论】:

阅读Caused by: java.lang.NoSuchMethodException 的行。那是你的实际错误 【参考方案1】:

您的com.example.ates.aaa.MyWebView 类缺少一个以ContextAttributeSet 作为参数的构造函数。您希望在布局资源中使用的所有 View 子类都需要该构造函数,例如 this one on WebView

【讨论】:

感谢解决的错误,为什么在构造函数中仅作为上下文参数时会出现错误? @ATES: LayoutInflater,以及支持它的代码,需要双参数构造函数。 即使我的 FATAL EXCEPTION stacktrace 没有说 我的 FATAL EXCEPTION stacktrace 说“由:java.lang.NoSuchMethodException: [class android.content.Context, interface android.util.AttributeSet]”,但在堆栈跟踪中显示在我自己的代码中由“at com.example.MyActivity.onCreate(MyActivity.java:78)”触发,其中第 78 行实际调用了“setContentView(R.layout.my_activity);”。这个例外非常具有误导性,但这个答案非常有帮助。

以上是关于WebView 中的“android.view.InflateException: Binary XML file Error inflating class”错误的主要内容,如果未能解决你的问题,请参考以下文章

RecyclerView中的WebView,滚动问题

android webview中的按钮弹框无法点击

iOS中的webView加载HTML

Webview 中的声音

Android webview中的js如何监听键盘事件

scrollView 中的 webView 不能滚动?