Why does using the Binding Interface resut in a ClassNotFound exception?

狂风中的少年 提交于 2019-12-10 15:28:56

问题


I am trying to create a custom attribute for any Android View.

I saw in this post that I could do it with the Data Binding Library. The Data Binding Guide and the post explain the layout should begin with a <layout> tag, but when inflating it I get the following exception:

05-06 22:36:31.294 11916-11916/com.plnech.example E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.plnech.example, PID: 11916
android.view.InflateException: Binary XML file line #1: Binary XML file line #1: Error inflating class layout
at android.view.LayoutInflater.inflate(LayoutInflater.java:
at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:378)
at android.widget.ArrayAdapter.getView(ArrayAdapter.java:369)
at android.widget.AbsListView.obtainView(AbsListView.java:2346)
at android.widget.ListView.measureHeightOfChildren(ListView.java:1280)
at android.widget.ListView.onMeasure(ListView.java:1188)
at android.view.View.measure(View.java:18788)
at android.widget.RelativeLayout.measureChild(RelativeLayout.java:676)
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:479)
at android.view.View.measure(View.java:18788)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:135)
at android.view.View.measure(View.java:18788)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
at android.support.v7.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:391)
at android.view.View.measure(View.java:18788)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at android.view.View.measure(View.java:18788)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:
at android.widget.LinearLayout.measureVertical(LinearLayout.java:748)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:630)
at android.view.View.measure(View.java:18788)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at com.android.internal.policy.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2643)
at android.view.View.measure(View.java:18788)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2100)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1216)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1452)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1107)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6013)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:858)
at android.view.Choreographer.doCallbacks(Choreographer.java:670)
at android.view.Choreographer.doFrame(Choreographer.java:606)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:44)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: android.view.InflateException: Binary XML file line #1: Error inflating class layoutat android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:776)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
at android.view.LayoutInflater.inflate(LayoutInflater.java:423) 
at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:378) 
at android.widget.ArrayAdapter.getView(ArrayAdapter.java:369) 
at android.widget.AbsListView.obtainView(AbsListView.java:2346) 
at android.widget.ListView.measureHeightOfChildren(ListView.java:1280) 
at android.widget.ListView.onMeasure(ListView.java:1188) 
at android.view.View.measure(View.java:18788) 
at android.widget.RelativeLayout.measureChild(RelativeLayout.java:676) 
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:479) 
at android.view.View.measure(View.java:18788) 
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951) 
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194) 
at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:135)
at android.view.View.measure(View.java:18788) 
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951) 
at android.support.v7.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:391) 
at android.view.View.measure(View.java:18788) 
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951) 
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194) 
at android.view.View.measure(View.java:18788) 
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951) 
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1465)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:748) 
at android.widget.LinearLayout.onMeasure(LinearLayout.java:630) 
at android.view.View.measure(View.java:18788) 
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951) 
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194) 
at com.android.internal.policy.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2643) 
at android.view.View.measure(View.java:18788) 
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2100) 
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1216) 
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1452) 
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1107) 
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6013) 
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:858) 
at android.view.Choreographer.doCallbacks(Choreographer.java:670) 
at android.view.Choreographer.doFrame(Choreographer.java:606) 
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:844)
at android.os.Handler.handleCallback(Handler.java:739) 
at android.os.Handler.dispatchMessage(Handler.java:95) 
at android.os.Looper.loop(Looper.java:148) 
at android.app.ActivityThread.main(ActivityThread.java:5417) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
Caused by: java.lang.ClassNotFoundException: Didnt find class "android.view.layout" on path: DexPathList[[zip file "/data/app/com.plnech.example-2/base.apk"],nativeLibraryDirectories=[/data/app/com.plnech.example-2/lib/x86, /vendor/lib, /system/lib]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
at java.lang.ClassLoader.loadClass(ClassLoader.java:469)
at android.view.LayoutInflater.createView(LayoutInflater.java:583)

Here is the layout in question:

<?xml version="1.0" encoding="utf-8"?>
<layout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/tools">
    <LinearLayout android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TextView android:id="@android:id/text1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:attribute="name"/>
    </LinearLayout>
</layout>

Here is the relevant part of my build.gradle:

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.1.0'
        classpath 'com.android.databinding:dataBinder:1.0-rc4'
    }
}
...

android {
    dataBinding {
        enabled true
    }
}
...

Why does this layout cause an error when being inflated?


回答1:


You need to include <data> inside your <layout>:

<layout 
   xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:app="http://schemas.android.com/tools">

   <data>    
   </data>

   <LinearLayout android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

      <TextView android:id="@android:id/text1"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          app:attribute="name"/>
    </LinearLayout>

</layout>

On a side note, classpath 'com.android.databinding:dataBinder:1.0-rc4' and apply plugin: 'com.android.databinding' are no more needed if you are using gradle version 2.+.



来源:https://stackoverflow.com/questions/37081859/why-does-using-the-binding-interface-resut-in-a-classnotfound-exception

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!