Error using databinding in android

后端 未结 12 871
情深已故
情深已故 2020-12-16 09:45

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\         


        
相关标签:
12条回答
  • 2020-12-16 10:06

    According to the current version of the guide (2015-11-11) you just have to add two dependencies: In the file build.gradle of the project add the gradle dependency. Currently the most recent version is 1.5.0-beta3

    dependencies {
            classpath 'com.android.tools.build:gradle:1.5.0-beta3'
        }
    

    And in the file build.gradle of the module include the dataBinding section:

    android{
      ...
      dataBinding {
            enabled = true
        }
      ...
    }
    

    The error

    error: package my.package.name.databinding does not exists

    may ocurr because a mismatch between the xml and the POJO class

    0 讨论(0)
  • 2020-12-16 10:09

    In my case, the problem was that I was adding layout_height and layout_width to the layout tag and also to my root layout. I've just removed both attributes from the layout tag and solved the problem.

    0 讨论(0)
  • removing apply plugin: 'com.neenbedankt.android-apt' from my build.gradle solved my problem.

    0 讨论(0)
  • 2020-12-16 10:10

    Looks like you forgot to add data binding plugin to your app's gradle file:

    apply plugin: 'com.android.databinding'

    You have to apply it to any module you want to use data binding.

    Also, you should not add layout_w/h to the layout tag, it is just an Android Studio bug, already fixed in aosp.

    0 讨论(0)
  • 2020-12-16 10:14

    You have to be very careful that your POJO class data members should be public if you are not creating the getter setters.

    If you have simple POJO without any getter or setter

    public class User {
       public final String firstName;
       public final String lastName;
       public User(String firstName, String lastName) {
           this.firstName = firstName;
           this.lastName = lastName;
       }
    }
    

    If you make getter setter, javaBean style class

    public class User {
       private final String firstName;
       private final String lastName;
       public User(String firstName, String lastName) {
           this.firstName = firstName;
           this.lastName = lastName;
       }
       public String getFirstName() {
           return this.firstName;
       }
       public String getLastName() {
           return this.lastName;
       }
    }
    
    0 讨论(0)
  • 2020-12-16 10:20

    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
    
    0 讨论(0)
提交回复
热议问题