简体   繁体   中英

Data binding expression not compiling

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

在数据标记内部,您还需要添加:

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

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

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
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM