<?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. yii2 windows 安装

    Yii是一个高性能的,适用于开发WEB2.0应用的PHP框架. Yii自带了丰富的功能 ,包括MVC,DAO/ActiveRecord,I18N/L10N,缓存,身份验证和基于角色的访问控制,脚手架, ...

  2. python&MongoDB

    1 #!/usr/bin/python 2 #-*- coding:utf-8 3 import pymongo 4 connection=pymongo.MongoClient() 5 tdb=co ...

  3. html5 --基础笔记2

    1.autocomplete 可以给表单本身(不是fieldset)设置属性来禁用整个表单的自动完成功能 <form id="" method="" au ...

  4. 《C++ Primer》之重载操作符与转换(下)

    转换与类类型 可用一个实参调用的非 explicit 构造函数定义一个隐式转换.当提供了实参类型的对象而需要一个类类型的对象时,编译器将使用该转换.这种构造函数定义了到类类型的转换.除了定义到类类型的 ...

  5. JavaScript高级程序设计:第二十一章

    第二十一章 Ajax与Comet 一.XMLHttpRequest对象 1.XHT的用法 在使用XHR对象时,要调用的第一个方法时open( ),它接受3个参数:要发送的请求的类型.请求的URL和表示 ...

  6. glusterfs——volume管理

    Q: 常用的命令有哪些? 创建volume: gluster volume create NAME stripe SCOUNT replica RCOUNT transport TYPE  BRICK ...

  7. AC_CONFIG_HEADER

    configure.in里有宏AC_CONFIG_HEADER()时用. AC_CONFIG_HEADER宏用于生成config.h文件,以便autoheader使用.

  8. iOS 2x 3x

    iOS中: 备注: Retina是一种显示技术,可以将把更多的像素点压缩至一块屏幕里,从而达到更高的分辨率并提高屏幕显示的细腻程度.而其最初该技术是用于苹果的iPhone4上.其屏幕分辨率为960×6 ...

  9. angular跨域访问的问题

    CORS跨域资源共享 跨域资源共享(CORS )是一种网络浏览器的技术规范,它为Web服务器定义了一种方式,允许网页从不同的域访问其资源. Form responseHeaders = (Form) ...

  10. Linux SSH使用公钥私钥实现免登陆

    公钥和私钥(我是文盲,钥字之前都是读yao,这是多音字这里应该念yue),是成对出现的,一旦任何一个做了更改都会验证失败. 1.免登陆的实现:   使用下例中ssky-keygen和ssh-copy- ...