android: data binding error: cannot find symbol class

后端 未结 26 2819
生来不讨喜
生来不讨喜 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 05:55

    Please refer to the android developer guide

    Layout file was main_activity.xml so the generate class was MainActivityBinding

    Since your xml is named "activity_contact_list.xml", you should use ActivityContactListBinding instead of the original one

    0 讨论(0)
  • 2020-12-14 05:57

    After ensuring the naming conventions are correct as described in other answers, and also trying to invalidate the cache and restart, deleting temp/cache folders the issue still persisted for me.

    I got rid of it as follows: Add a new dummy XML resource. This will trigger bindings and its meta-data to re-create across the project. The annoying compile errors should no longer be visible anymore. You now delete the dummy XML you added.

    For me as of August 2020, the Binding would automatically get corrupted repeatedly. It seems to be biting more than it can chew under the hood.

    0 讨论(0)
  • 2020-12-14 05:58

    Actually it can be happend for various reason and for poor logging mechanism in data binding it is very hard to find the reason.So go got the proper error first go to the terminal and run the following command-

    gradlew :app:build --stacktrace
    

    It will show you the proper error with the number of line in XML where error is found.

    For example -

    ERROR: Could not find accessor com.example.model file://app\src\main\res\layout\fragment_example.xml Line:91
    
    0 讨论(0)
  • 2020-12-14 05:58

    I fell into this issue because my Activity was called MainActivityMVVM and the Binding was converted into MainActivityMvvmBinding instead of MainActivityMVVMBinding. After digging into the generated classes I found the issue.

    0 讨论(0)
  • 2020-12-14 05:59

    I consistently run into this problem. I believe it has to do with android studio not being aware of dynamically generated files. If you have everything else right for databinding try to File > Invalidate Caches/Restart... and select Invalidate Caches and Restart. Then try and import the BR file... it should import fine.

    You may have to throw in a Clean and Rebuild.

    0 讨论(0)
  • 2020-12-14 05:59

    Improper package names can also cause the above error. As of Android Gradle plugin 3.2 (as far as I can tell) CamelCase package names will be inferred improperly as classes, which will break the generated binding object.

    Example:

    src
    |
    -> FooPackage
        |
        -> Bar.java
    

    will be generated wrongly as

    import src.FooPackage
    
    ...
    
    public abstract class MyBinding extends ViewDataBinding {
        @NonNull
        public final FooPackage.Bar mInstance;
    ...
    }
    

    This obviously doesn't make any sense.

    Refactor FooPackage to foopackage according to java conventions and be saved. You will then get:

    import src.foopackage.Bar
    
    ...
    
    public abstract class MyBinding extends ViewDataBinding {
        @NonNull
        public final Bar mInstance;
    ...
    }
    
    0 讨论(0)
提交回复
热议问题