Reputation: 3591
I'm trying out the new data binding library. I have a weird issue where binding the visibility
property is not compiling.
This is a simplified version of the xml file:
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<data>
<variable
name="header"
type="com.example.EmailHeader" />
</data>
<RelativeLayout ... >
<TextView
...
android:text="@{header.senderName ?? header.senderAddress}"
android:visibility="@{header.hasAttachment ? View.VISIBLE : View.INVISIBLE}" />
</RelativeLayout>
</layout>
I get the follow message when compiling:
Error:Execution failed for task ':app:compileDebugJavaWithJavac'.
java.lang.RuntimeException: Found data binding errors. ****/ data binding error ****msg:Identifiers must have user defined types from the XML file. View is missing it
Everything compiles (and works!) when I remove the android:visiblity
declaration.
I don't see what I'm missing here
Upvotes: 62
Views: 24959
Reputation: 27255
Problem persisted despite adding <import type="android.view.View" />
to my data tag.Finally found the error to be caused by a mismatch of my variable name and object of my POJO class.
This was my data tag:
<data>
<import type="android.view.View" />
<variable
name="employee"
type="com.example.Employee"/>
</data>
and I was using:
<TextView
...
android:text="@{user.lastName}" />
instead of:
<TextView
...
android:text="@{employee.lastName}" />
Forgot to change it after copying code from documentation. Look out for mistakes like this which are hard to detect for newbies to DataBinding
Upvotes: 31
Reputation: 1921
I faced the exact same error which was caused by the fact that the POJO object was in a library project.
Just upate the build.gradle of the library to enable databinding as well as in the main project:
dataBinding {
enabled = true
}
Upvotes: 7
Reputation: 2671
Inside of the data tag you need to also add:
<import type="android.view.View" />
Upvotes: 169