为什么在ClassNotFound异常中使用绑定接口?

时间:2016-05-06 21:43:40

标签: android android-layout classnotfoundexception android-databinding

我正在尝试为任何Android View创建自定义属性。

我在this post中看到我可以使用数据绑定库来完成它。 Data Binding Guide和帖子解释布局应该以{{1​​}}标记开头,但在对其进行充气时,我会遇到以下异常:

<layout>

以下是有问题的布局:

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)

以下是我<?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> 的相关部分:

build.gradle

为什么这种布局在充气时会导致错误?

1 个答案:

答案 0 :(得分:3)

您需要在<data>

中加入<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>

另外,如果您使用的是gradle版本classpath 'com.android.databinding:dataBinder:1.0-rc4',则不再需要apply plugin: 'com.android.databinding'2.+