【发布时间】:2015-08-28 00:39:46
【问题描述】:
我正在尝试按照指南 here 中的说明测试数据绑定。 我已将其包含在我的 build.gradle 文件(模块应用程序)中:
compileSdkVersion 'android-MNC'
buildToolsVersion '23.0.0 rc2'
在项目 build.gradle 文件中,我已将其包含在我的依赖项中:
classpath "com.android.tools.build:gradle:1.3.0-beta2"
classpath "com.android.databinding:dataBinder:1.0-rc0"
布局文件与指南中给出的完全相同。
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<data>
<variable name="user" type="com.example.User"/>
</data>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@{user.firstName}"/>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@{user.lastName}"/>
</LinearLayout>
</layout>
最初它给出了错误Element layout does not have the required attribute layout_width and layout_height。
我试图通过将 match_parent 分配给两者来修复它。然后我得到了错误
Error parsing XML: duplicate attribute 在我将 layout_height 和 layout_width 分配给线性布局的行上。
我再次尝试通过删除这些属性来解决此问题。现在每次我尝试编译时,我都会看到这个-error: package my.package.name.databinding 不存在。
代码完成在我尝试使用此布局的片段中完美运行。
那么我错过了什么?
【问题讨论】:
-
你能发布你的
userPOJO 类和Activity类 -
在 xml
type属性中仔细检查包引用。my.package.User或com.example.User -
我遇到了这个错误,最后发现这是因为我有一个重复的 android xml 命名空间。 Android Studio 中的 DataBinding 库让你知道问题出在哪里实在是太糟糕了。希望下一次更新能够解决问题。
-
您也可以查看this教程供您参考。
标签: android android-fragments data-binding