Error using databinding in android

后端 未结 12 872
情深已故
情深已故 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:23

    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
    0 讨论(0)
  • 2020-12-16 10:26

    You may have a problem or mismatch between your model and your layout file. In my case I had:

    ...
    <TextView
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true"
                android:layout_toLeftOf="@+id/photo"
                android:layout_toStartOf="@+id/photo"
                tools:text="0912454433"
                android:textStyle="bold"
                android:text="@{contact.cellPhoneNumber}"
                android:layout_below="@+id/name"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
    

    but my model class was like so:

    public class MyContact {
        public String name;
        public String cellphoneNumber; // <-- Notice the lowercase 'p'
    }
    
    0 讨论(0)
  • 2020-12-16 10:27

    I also suffer with the same problem. But my mistake is

    User class variables declared as not a public. So we should declare the POJO class variables as public.

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

    Then solved my problem.

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

    This appears to be an issue in android studio. The editor will flag it as an error however it will still compile.

    I had the same issue and so I cloned this example repo to test my system is configured correctly to support the databinding api https://github.com/mgrzechocinski/android-bindings-example

    Ignore the error warning on the layout tag. It is wrong.

    Something I noticed is you don't necessarily require the data tag if are simply using the databinding to get access to the views items through the BindingActivity class that is generated.

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

    If you are using 'android-apt' plugin for dagger or other libraries, after enabling data binding, you have to remove 'android-apt' plugin and use 'provided' instead of 'apt' in your dependencies. For example if you are using dagger, remove this

    classpath 'com.neenbedankt.gradle.plugins:android-apt:1.4'
    

    and this

    apply plugin: 'com.neenbedankt.android-apt' 
    

    and then replace

    apt 'com.google.dagger:dagger-compiler:2.0'
    

    with

    provided 'com.google.dagger:dagger-compiler:2.0'
    
    0 讨论(0)
  • 2020-12-16 10:33

    // This is under activity_main xml .

      <data>
      <variable
          name="student"
          type="com.example.vishalkamboj.testdatabinding.Student"    />
    </data>
    

    // This is Student Class

    public class Student {
    

    public String firstname ; public String lastname ;

    public Student(String firstname , String lastname)
    {
        this.firstname = firstname;
        this.lastname = lastname;
    
    }
    

    }

    0 讨论(0)
提交回复
热议问题