Data binding expression not compiling - android

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

Inside of the data tag you need to also add:
<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
}

Related

Unable to parse XML - Kotlin

Caused by:
org.gradle.workers.internal.DefaultWorkerExecutor$WorkExecutionException: A
failure occurred while executing com.android.build.gradle.internal.res.ParseLibraryResourcesTask$ParseResourcesRunnable
Other error codes
FAILURE: Build completed with 2 failures.
1: Task failed with an exception.
What went wrong:
Execution failed for task ':app:parseDebugLocalResources'.
A failure occurred while executing com.android.build.gradle.internal.res.ParseLibraryResourcesTask$ParseResourcesRunnable
Failed to parse XML file '/home/shinto/Documents/PostMethodTrialChecking/HelpIntern/app/build/intermediates/packaged_res/debug/layout/fragment_signup.xml'
fragment_signup.xml
<data>
<variable
name="users"
type="com.shinto.helpintern.MainViewModel" />
</data>
android:visibilities="#{users.}"
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/progBar"
android:layout_width="match_parent"
android:layout_height="match_parent">
Your XML code is wrong. And as the error suggests, compiler expects a <, means an opening tag. Why?
Because this line
android:visibilities="#{users.}" is misplaced and isn't in any tag. Even if it was in its correct position, it wouldn't work, because there's nothing called visibilities, it's visibility.
I assume you want to toggle the visibility of the ContraintLayout based on any (assuming boolean) value of the data. To do that, change your code as:
<data>
<import type="android.view.View"/>
<variable
name="users"
type="com.shinto.helpintern.MainViewModel" />
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/progBar"
android:visibility="#{users.yourValue? View.GONE : View.VISIBLE}"
android:layout_width="match_parent"
android:layout_height="match_parent">
This will toggle the layout's visibility based on the value of your model.

Didn't find class "androidx.constraintlayout.ConstraintLayout"

I am developing a library for Android, and this library has some activities with its layouts.
In the layout, I have the next code
...
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.google.android.material.floatingactionbutton.FloatingActionButton
...
I am using Androidx.
An I get the following error:
Error inflating class android.support.constraint.ConstraintLayout
The first attempt to solve this was to changeandroid.support.constraint.ConstraintLayout to androidx.constraintlayout.widget.ConstraintLayout
But when I do this I get the following error:
androidx.constraintlayout.widget.ConstraintLayout cannot be cast to
androidx.constraintlayout.widget.Group
So I try to change to androidx.constraintlayout.widget.Group
I get the following error:
androidx.constraintlayout.widget.Group cannot be cast to
android.view.ViewGroup
I did a refactor again to androidx and then I get the following error:
Didn't find class "androidx.constraintlayout.ConstraintLayout"
Any idea?
Thanks
Follow this steps
First add below dependencies in your Build.Gradle file
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
Use this ConstraintLayout like this
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</androidx.constraintlayout.widget.ConstraintLayout>
Make sure you have correct imports of ConstraintLayout for in your activity if you have used
import androidx.constraintlayout.widget.ConstraintLayout

Exception during databinding generation after upgrading to gradle plugin 3.0.1

I upgraded a project to Gradle plugin 3.0.1, but during the databinding classes generation, I get the following error (and the binding classes are not generated):
error: cannot generate view binders java.lang.NullPointerException
at android.databinding.tool.store.SetterStore.getMatchingMultiAttributeSetters(SetterStore.java:642)
at android.databinding.tool.store.SetterStore.getMultiAttributeSetterCalls(SetterStore.java:529)
at android.databinding.tool.BindingTarget.resolveMultiSetters(BindingTarget.java:221)
at android.databinding.tool.LayoutBinder.<init>(LayoutBinder.java:249)
at android.databinding.tool.DataBinder.<init>(DataBinder.java:52)
at android.databinding.tool.CompilerChef.ensureDataBinder(CompilerChef.java:90)
at android.databinding.tool.CompilerChef.sealModels(CompilerChef.java:203)
at android.databinding.annotationprocessor.ProcessExpressions.writeResourceBundle(ProcessExpressions.java:193)
at com.sun.tools.javac.processing.JavacProcessingEnvironment.callProcessor(JavacProcessingEnvironment.java:794)
...
After some investigation, it seems that the binding class is correctly generated if I have at most 1 attribute using data binding on a given element in a layout xml. If I add a second one, I get the previous exception.
In the context of my project, the following view causes the error, but unfortunately, in a brand new project, it compiles normally.
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
<import type="android.view.View"/>
<variable
name="title"
type="String"/>
<variable
name="visible"
type="boolean"/>
</data>
<RelativeLayout android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- with either text or visiblility, the project builds, but not with both -->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#{title}"
android:visibility="#{visible ? View.VISIBLE : View.GONE}"
/>
</RelativeLayout>
</layout>
Has anyone encountered this issue ?

Android Studio shows errors in layout.xml

I'm using data binding library and I experience following issues in Android Studio 3.0:
somelayout.xml:
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<data>
<variable
name="meeting"
type="some.package.MeetingStatusResponse"/>
</data>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#{meeting.title}"
tools:text="Title"
/>
...
</LinearLayout>
</layout>
In this place: android:text="#{meeting.title}" Android Studio 3.0 underlines the # symbol and informs about error:
Error:(29, 27) <expr> or <lambda expression> expected, got '#'
Model class is written in Kotlin if it has any relevance.
The code completion is also not working for bound classes. The application can be successfully build using both, gradle command and Android Studio run app button. So this is just editor issue.
I observe this issue on Mac. On Windows it works ok. I do not have more computers to check if it's platform related issue.
Maybe you forgot to add in gradle compileOptions ?
`sourceCompatibility JavaVersion.VERSION_1_7`
`targetCompatibility JavaVersion.VERSION_1_7`

How can I use the Android library ExtendedCalendarView

I am new in Android-Programming and I and my programmer team have to program a little app for our college. So we need the ExtendedCalendarView-Library and we already imported it in our AndroidStudio-project but we always get the same failure:
Can not find the following classes com.tyczj.extendedcalendarview.ExtendedCalendarView
try to fix Build Path
and so on.
Code in our test.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<com.tyczj.extendedcalendarview.ExtendedCalendarView
android:layout_width="match_parent"
android:layout_height="match_parent">
</com.tyczj.extendedcalendarview.ExtendedCalendarView>
</RelativeLayout>
But we have imported another library at the same way and it works. We also get a suggestion from com.tyczj.extendedcalendarview.ExtendedCalendarView when we open the '<' in a .xml file but always the same failure.
Meanwhile time is pressing so we have not so much time anymore.
Could anyone help us?
Have you copied the provider tag in your android manifest if not then copy it in application tag of AndroidManifest:
<application>
...
<provider
android:name="com.tyczj.extendedcalendarview.CalendarProvider"
android:authorities="com.tyczj.extendedcalendarview.calendarprovider" />
</application>

Categories

Resources