Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error using databinding in android

I am trying to test data binding as given in the guide here. I have included this in my build.gradle file (of module app) :

compileSdkVersion 'android-MNC'
buildToolsVersion '23.0.0 rc2'

In the project build.gradle file, I have included this in my dependencies :

classpath "com.android.tools.build:gradle:1.3.0-beta2"
classpath "com.android.databinding:dataBinder:1.0-rc0"

The layout file is exactly the same as the one given in the guide.

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<data>
   <variable name="user" type="com.example.User"/>
</data>
<LinearLayout
   android:orientation="vertical"
   android:layout_width="match_parent"
   android:layout_height="match_parent">
   <TextView android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="@{user.firstName}"/>
   <TextView android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="@{user.lastName}"/>
</LinearLayout>
</layout>

Initially it gave an error Element layout does not have the required attribute layout_width and layout_height.

I tried to fix it by assigning match_parent to both. Then I got the error Error parsing XML: duplicate attribute on the lines where i assigned layout_height and layout_width to the linear layout.

Again I tried to fix this by removing these attributes. Now every time I try to compile, I see this- error: package my.package.name.databinding does not exist.

The code completion is working perfectly in my Fragment where I am trying to use this layout.

So what did I miss ?

like image 405
axay Avatar asked Jun 12 '15 05:06

axay


People also ask

Is Android databinding deprecated?

Recently Android has announced that with Kotlin 1.4. 20, their Android Kotlin Extensions Gradle plugin will be deprecated and will no longer be shipped in the future Kotlin releases. Android Kotlin Extensions plugin brought with it two very cool features : Synthetics let you replace calls to findViewById with kotlinx.

Can we use databinding in MVP?

Why use Data binding with Mvp? Combining Databinding along wih MVP pattern can result in a very clean structure and maintainable project. Databinding saves u a lot of stress and uneccesary long lines of code. Your UI is updated eaily and gone are those days where you need "findViewById" and onclick listeners and so on.


3 Answers

Your bindings in the xml file might be invalid.

Make sure to double check

  1. Are type attributes valid reference to data object? type="my.package.Class"
  2. Are the binding valid? name="client" -> "@{client.field}"
  3. Are data fields accessible? public or encapsulated with getters
  4. Are field names in java and the xml file matching, check for typos
  5. If you have renamed the xml file, make sure you also update the Binding objects. OldNameBinding -> NewNameBinding
  6. Clean Project
like image 184
J.G.Sebring Avatar answered Oct 28 '22 05:10

J.G.Sebring


removing apply plugin: 'com.neenbedankt.android-apt' from my build.gradle solved my problem.

like image 21
sakirk Avatar answered Oct 28 '22 04:10

sakirk


it’s usually because there is an error in your XML layout file and it can’t generate the binding object. Make sure you write data type correctly, check lower or upper of your writing.

    <data>
        <variable name="yourObject" type="com.example.simple.YourObject"/>
    </data>

And make sure object is right calling:

    <TextView android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@{yourObject.name}"/>
    <TextView
like image 3
Lay Leangsros Avatar answered Oct 28 '22 05:10

Lay Leangsros