<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
/> </LinearLayout>

1.反例:像这个布局文件,如果要使用DataBinding的话,最外层要改成layout布局的,

但是我以为它最终要改成layout,所以我事先在加入complie和enabled代码前直接改成layout,

    //DataBinding
compile "com.jakewharton.rxbinding:rxbinding:0.4.0"
compile "com.jakewharton.rxbinding:rxbinding-design:0.4.0"
    compileSdkVersion 24
buildToolsVersion "24.0.0"
dataBinding {
enabled = true
}
}

导致在在加入complie和enabled代码后,点击右上角的Sync Now,一直报错

C:\android\ASWorkSpace\HighlyTechnical\tenical\build\intermediates\data-binding-layout-out\debug\layout\activity_main.xml
Error:(11) Error parsing XML: duplicate attribute
Error:Execution failed for task ':tenical:processDebugResources'.
> com.android.ide.common.process.ProcessException: Failed to execute aapt
Information:BUILD FAILED

莫名奇妙的错误。

搞了半天都不知道是什么原因,

原来是我们要将根布局变成layout,还要去掉根布局的width和height

 <?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/tv_test"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!" />
</RelativeLayout> </layout>

2.

(1)获取到的DataBinding对象,是根据布局文件命名的

ListItemHomeBinding binding = DataBindingUtil.inflate(LayoutInflater.from(UIUtils.getContext()), R.layout.list_item_home, parent, false);

而布局文件要先实现根布局为layout,

 <?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
>
<data>
<variable
name="ListBean"
type="com.weizh_000.googleplay.domain.HomeBean.ListBean"/> </data> <TextView
android:text="@{ListBean.downloadUrl}"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#000"
android:id="@+id/textView" />
</layout>

然后build一遍,对象名就出来了,但是按ctrl+2+L出来的不是这个名,是

ViewDataBinding,直接用这个也可以,或者自己手动敲对象名。

(2)不仅对象名会创建,对象方法也会被创建。对象方法名是根据data标签中
variable标签的name创建的,例如上面布局文件中的name为ListBean,那么就会创建一个方法名字为setListBean
         @Override
public View getView(int position, View convertView, ViewGroup parent) {
ListItemHomeBinding binding;
if (convertView == null) {
binding = DataBindingUtil.inflate(LayoutInflater.from(UIUtils.getContext()), R.layout.list_item_home, parent, false);
convertView = binding.getRoot();
convertView.setTag(binding);
} else {
binding = (ListItemHomeBinding) convertView.getTag();
} binding.setListBean(getItem(position));
return convertView; }

这是ListView的Adapter中的getView方法,同时也展示了如何在ListView中使用DataBinding。

3.要有data的标签,而data的标签又要有一个类名,所以又得创建一个类,通常这个类是javabean类

DataBinding注意事项Error parsing XML: duplicate attribute以及如何在listview中使用DataBinding的更多相关文章

  1. activiti 发布异常 org.activiti.engine.ActivitiException: Error parsing XML

    三月 23, 2015 1:58:31 下午 org.apache.catalina.core.StandardWrapperValve invoke 严重: Servlet.service() fo ...

  2. Android 编译错误——布局 Error parsing XML: not well-formed (invalid token)

    在修改了Android布局文件后,编译出现Error parsing XML: not well-formed (invalid token). 首先先排查xml文件的编码格式是否为UTF-8, &l ...

  3. Error parsing XML: junk after document element这样的错误 - CSDN博客

    很多开发者可能在编写XML布局文件时提示Error parsing XML: junk after document element这样的错误,这里Android123提示大家一般合法的XML文件只有 ...

  4. android studio 报 Error:(79) Error parsing XML: not well-formed (invalid token)

    android studio 报 Error:(79) Error parsing XML: not well-formed (invalid token) 我的原因是因为string 里面有< ...

  5. [Error] Error parsing XML: unbound prefix

    发生该错误的代码: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:an ...

  6. Error parsing XML: not well-formed (invalid token) 报错+R文件消失解决的方法

    xml报错: 这个xml文件上右键source ->format 注意:res下的文件名称不能大写 R文件消失: 在攻克了其它问题的情况下(或者其它问题还没解决先凝视掉) 手动删除gen pro ...

  7. Error parsing XML: not well-formed (invalid token)

    从网络上或别的文件复制粘贴进来的代码有隐含格式,可将内容先粘贴进记事本清除格式,再复制粘贴进工程文件,即可解决此问题 注:1. 要使工程文件全选清空, 2. 若粘贴后刷新仍无效果,可手动输入

  8. Android - Error parsing XML: unbound prefix

    概述 这个问题,虽然看起来不是问题,但是如果不知道的人,还会花点时间,有的人甚至重新安装ADT. 我一开始还以为是排版的问题(Layout),因为初学,弄来弄去,最好还是到网上搜. 其实就不是什么问题 ...

  9. Error parsing XML: not well-formed (invalid token) 报错

    鼠标右键选择Source然后再选Format 就可以解决此问题

随机推荐

  1. iptables指令参考

    1.首先介绍一下指令和相关配置文件 启动指令:service iptables start 重启指令:service iptables restart 关闭指令:service iptables st ...

  2. PHP中require和include的区别

    include()与require()的功能相同 include(include_once) 与 require(require_once)都是把把包含的文件代码读入到指定位置来,但是二者再用法上有区 ...

  3. JPA 系列教程17-继承-独立表-TABLE_PER_CLASS

    PerTable策略 每个具体的类一个表的策略 举例 这种映射策略每个类都会映射成一个单独的表,类的所有属性,包括继承的属性都会映射成表的列. 这种映射策略的缺点是:对多态关系的支持有限,当查询涉及到 ...

  4. 2.Perl 多线程:Threads(线程返回值)

    use warnings; use strict; use threads; sub TEST{ print "Hello, World!\n"; 'a'/); } #返回列表方法 ...

  5. android之服务service

    service的概念: 官方定义:在后台长时间的操作,没有用户界面,不与用户进行交互,在默认的情况下,service运行在应用程序进程的主线程中,如果需要在Service中处理一些网络连接等耗时操作, ...

  6. FZU 1502 Letter Deletion

    最长公共子序列. #pragma comment(linker, "/STACK:1024000000,1024000000") #include<cstdio> #i ...

  7. [转载]Linux 环境下编译 0.11版本内核 kernel

    最近在看<.如果Clobber/Modify 为空,则其前面的冒号(:)必须省略. 2.如果Output,Input,Clobber/Modify都为空,Output,Input之前的冒号(:) ...

  8. Chapter 2 Open Book——6

    Last night I'd discovered that Charlie couldn't cook much besides friedeggs and bacon. 昨天晚上我终于发现查理除了 ...

  9. LeetCode OJ 337. House Robber III

    The thief has found himself a new place for his thievery again. There is only one entrance to this a ...

  10. tar解压

    tar在linux上是常用的打包.压缩.加压缩工具,他的参数很多,折里仅仅列举常用的压缩与解压缩参数 参数: -c :create 建立压缩档案的参数:-x : 解压缩压缩档案的参数:-z : 是否需 ...