android: data binding error: cannot find symbol class

后端 未结 26 2842
生来不讨喜
生来不讨喜 2020-12-14 05:49

I am getting started for using DataBinding feature. I am facing problem with it.

Error:(21, 9) error: cannot find symbol class ContactL

相关标签:
26条回答
  • 2020-12-14 06:00

    You can see Main Answer for complete information about data binding errors and solutions. I will try to tell some important points below.

    5 Tips that I use to solve binding issues.

    Assuming that you have enabled data binding in build.gradle and you have converted your layout to binding layout.

    First of all binding class auto generate when you convert layout to binding layout. Still sometimes it is not generated when background threads are not working. Here are some tips for you to resolve this.

    (1) Name of generated class

    Layout name is in snake_case activity_main.xml

    Data binding class will be in PascalCase like ActivityMainBinding.

    (2). Type full name of Binding class

    I felt sometimes when you type ActivityMai..., then it does not show suggestion, but that does not mean class is not generated. In that case you should type full name of expected generated class. Like type ActivityMainBinding and it will show import popup. (That's what i faced many times.)

    Still not getting import suggestion. Try manual import.

    import <yourpackage>databinding.ActivityMainBinding;
    

    (3). Rebuild Project

    If still your class is not generated. (Some time when we paste layout file, then it happens). Then Rebuild Project from Build> Rebuild (Not Build or Make project). It will generate your data binding class. (Rebuild does Magic for me all times.)

    (4) If you have created an <variable in your layout and it does not show up its setter and getter in data binding class, then follow 4th point.

    (5) Still if your class is not generating then you should check if build is not failing due to an error in your layout file. Data binding class will generate with a successful build.

    This is all what i do to solve my data binding errors. If you get any further issue, you can comment here.

    Source answer

    0 讨论(0)
  • 2020-12-14 06:00

    For me it was an error in the layout xml binding, I had

    app:setNameString="@{person}"

    instead of

    app:nameString="@{person}"

    the type name must match the name you have set up in the @BindingAdapter class (if you are using binding adapter)

    0 讨论(0)
  • 2020-12-14 06:02

    In my project it was a trouble in:

    android:text="@{safeUnbox(viewmodel.population)}"
    

    So I've wrapped it in String.valueOf() :

    android:text="@{String.valueOf(safeUnbox(viewmodel.population))}"
    

    And it was resolved

    0 讨论(0)
  • 2020-12-14 06:03

    this is your code

    ContactListActivityBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_contact_list);
    

    Replace this code

    ActivityMainBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_contact_list);
    
    0 讨论(0)
  • 2020-12-14 06:03

    You need to declare and pass the binding in the activity layout, not in the included layout.

    Example from the documentation:

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

    Here, there must be a user variable in both the name.xml and contact.xml layout files.

    0 讨论(0)
  • 2020-12-14 06:03

    You must change your activity_contact_list to be binded - add layout tag as you did in content_contact_list. Don't forget, The root layout inside activity_contact_list must have an id for the Binding class to be generated and will be named ActivityContactListBinding (i.e. the name of the layout with camel casting instead of underscores).

    Next, inside activity_contact_list, give <include layout="@layout/content_contact_list" /> an id, then you will have access for its binding instance through your ActivityContactListBinding instance.

    Somthing like:

    binding.contentContactList.setContact(user);
    

    Let me know if it works.

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