Can't resolve Android databinding class

前端 未结 23 2143
南旧
南旧 2020-12-02 19:55

While using data binding , I am not able to get class MainActivityBinding as per Data Binding Guide

My layout name is activity_main.xml. I

相关标签:
23条回答
  • 2020-12-02 20:28

    Thanks to all for your answer.I found solution with ContentMainBinding class name for data binding. Lets me explain.

    NOTE: While using layout with <include ... here is <include layout="@layout/content_main" having Data Binding functionality, the class name related to include layout name. Here is the ContentMainBinding

    My layout file are as below:

    activity_main.xml

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.design.widget.CoordinatorLayout 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="match_parent"
        android:fitsSystemWindows="true"
        tools:context="com.databindingdemo.app.MainActivity">
        ...
        <include layout="@layout/content_main" />
        ...
        </android.support.design.widget.CoordinatorLayout>
    

    And content_main.xml is layout where I added my Data Binding layout code.

    So instead of using MainActivityBinding it can resolved with ContentMainBinding

    The code which work for me is below:

    //Code for data binding
        ContentMainBinding contentMainBinding = DataBindingUtil.setContentView(this, R.layout.content_main);
        user = new User("Pranay", "Patel", "demoemail@gmail.com", "9999999999");
        contentMainBinding.setUser(user);
    

    DONE.

    0 讨论(0)
  • 2020-12-02 20:33

    In my case DELETING the the app build folder and then rebuilding the project solved my problem.

    Even following steps did not work.

              1) Rebuild the project after adding the dataBinding  in gradle.
    
              2) Rebuild after adding layout tag in xml.
    
              3) cleaning the project
    
              4) rebuilding the project
    
              5) Restarted the Android Studio
    

    After enabling the dataBinding in app gradle file please rebuild. Then we can find Binding class generated.

              If the layout name is fragment_home, The Binding class name will be FragmentHomeBinding.
    
    0 讨论(0)
  • 2020-12-02 20:35

    DataBinding class will be generated based on your xml file name. It is clearly mentioned in doc you are following.

    By default, a Binding class will be generated based on the name of the layout file, converting it to Pascal case and suffixing “Binding” to it. The above layout file was main_activity.xml so the generate class was MainActivityBinding

    If your xml name is activity_main.xml than DataBinding class name will be ActivityMainBinding.

    If your xml name is main_activity.xml than DataBinding class name will be MainActivityBinding.

    Dont forget to clean and build project once

    you can follow this tutorial for more about DataBinding

    0 讨论(0)
  • 2020-12-02 20:36

    Restart Android studio or try to rename the XML file to another name and check if binding works once it works.

    0 讨论(0)
  • 2020-12-02 20:40

    I was facing the same issue,

    If your XML name is activity_main.xml than DataBinding class name will be ActivityMainBinding

    If it is correct check if you add below code in your xml file,

    <layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    
    <data>
        <variable
            name="xyz"
            type="com.example.yourapp.classname" />
       />
    
    </data>
    

    if either method is not the issue, try

    • clean project
    • Rebuild project
    0 讨论(0)
提交回复
热议问题