Data binding expression not compiling

AndroidData Binding

Android Problem Overview


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

Android Solutions


Solution 1 - Android

Inside of the data tag you need to also add:

<import type="android.view.View" />

Solution 2 - Android

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

Solution 3 - Android

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
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJordy LangenView Question on Stackoverflow
Solution 1 - AndroidbwhiteView Answer on Stackoverflow
Solution 2 - AndroidOjonugwa Jude OchalifuView Answer on Stackoverflow
Solution 3 - AndroidBenoitView Answer on Stackoverflow