数据绑定类未生成 [英] Data Binding class not generated
问题描述
当我的xml绑定类中使用<layout>
和<data>
时,我正在我的项目中使用数据绑定.
I am using Data Binding in my project, when using <layout>
and <data>
in my xml binding class is not generated.
例如,我有activity_main.xml
For example i have activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<data> </data>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
</RelativeLayout>
</layout>
现在,如果我在活动/片段中编写ActivityMainBinding
,它会显示错误,指出该类不可用.但是在我的xml文件中包含<variable>
之后,它可以生成ActivityMainBinding
类.
Now if i am writing ActivityMainBinding
in my activity/fragment it shows error that class is not available. But after including <variable>
in my xml file, it is able to generate ActivityMainBinding
class.
Android Studio: 2.1.3
类路径: com.android.tools.build:gradle:2.1.3
minSdkVersion 16
targetSdkVersion 24
buildToolsVersion 24.0.0
Android Studio : 2.1.3
Classpath : com.android.tools.build:gradle:2.1.3
minSdkVersion 16
targetSdkVersion 24
buildToolsVersion 24.0.0
推荐答案
我没有得到任何令人满意的答案.因此,这里有一些技巧,这些技巧是我的数据绑定知识的总结.
I did not get any satisfying answers. So here are the tips which are summary of my data binding knowledge.
为了获得更准确的错误和建议,我强烈建议将Android Studio和Gradle插件版本更新为最新版本.因为在AS 3.2版本之后我没有遇到很多问题.
To get more accurate errors and suggestions, I strongly recommend to update Android Studio and Gradle plugin version to the latest. Because I am not facing many issues after AS 3.2 version.
请参见最新的Android Studio 和
阅读此答案后,您不会陷入类和数据变量的数据绑定自动生成问题.
After reading this answer, you will not get stuck in data binding auto generation issues for both Classes and Data Variables. 一一检查这些点.这些都可以使您的工作完成. 最后的第三点非常重要,请不要错过它们. Check these points one by one. Any of these can make your work done. Point 3 to last are really important, so don't miss them. 您应该在 You should have data binding enabled in
现在,如果要生成数据绑定类,则应用数据绑定
Now if you want data binding class to be generated then you should wrap 接着检查绑定变量名称是否正确(如视图模型类中一样) Along with this check whether the binding variable names are correct as in the view model class 您的数据绑定类应在创建绑定布局后生成. Your data binding class should be generated after creating binding layout. 如果您的布局名称为蛇形 If your layout name is in snake case
有时,当您键入
Sometimes when you type
如果构建失败,则不会生成您的绑定类和布局中的新变量.因此,首先通过 Ctrl + F9(生成>创建项目) 创建项目. 我总是这样做,因为它比 I always do this because it takes much less time than 请注意,我更喜欢最近中的关闭并打开,因为它比重建/重新启动IDE 所需的时间要短得多>. Note that I prefer Close and Open from Recent because it takes much less time than Rebuild / Restart IDE. 如果仍然没有生成您的班级. (有时,当我们粘贴布局文件时,它会发生).然后从 If still your class is not generated. (Some time when we paste layout file, then it happens). Then Rebuild Project from 在将AS更新为 Android Studio 3.2 之后,我感觉很多数据绑定自动修复的错误一代.因此,您还应该拥有最新的AS. After updating AS to Android Studio 3.2, I felt many bugs fix in data binding auto generation. So you should also have the latest AS. 通常,当我们在布局中放置一个变量时,它会创建一个getter和setter方法.我们可以使用 Usually, when we put a variable in layout, it creates a getter and setter of it. And we can use 如果您在布局中创建了数据变量- If you have created a data variable - 如果您在布局中更改了某些 If you changed the type of some 最后,如果仍然没有生成您的绑定类,那么我们拥有最强大的武器. -重新启动Android Studio:D Finally if still your binding class is not generated, then we have our most powerful weapon. - Restart Android Studio:D 这是我为解决数据绑定错误而要做的全部工作.如果您还有其他问题,可以在这里发表评论. This is all that i do to solve my data binding errors. If you get any further issues, you can comment here. 这篇关于数据绑定类未生成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
build.gradle
中启用数据绑定.如果没有,则添加它并同步.build.gradle
. If not then add this and Sync.android {
...
dataBinding {
enabled = true
}
...
}
2.检查布局转换为绑定布局
xml layout
包裹( <layout
标记).像这样的东西.2. Check layout is converted in binding layout
xml layout
with data binding (<layout
tag). Something like this.<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.constraint.ConstraintLayout>
</layout>
activity_main.xml
,则为数据绑定类
将在驼峰式保护套中生成,例如ActivityMainBinding
.
activity_main.xml
then data binding class
will be generated in camel case like ActivityMainBinding
. 4.看不到导入建议?
ActivityMai...
时,它不显示建议,在这种情况下,手动导入. 4. Can't See Import Suggestion?
ActivityMai...
, then it does not show suggestion, in that case import manually. import <yourpackage>databinding.ActivityMainBinding;
5.阅读Build Fail Logcat
Rebuild
/Make
项目花费的时间要少得多.Rebuild
/ Make
project.
Build> Rebuild
(不生成或创建项目)中重建项目.它将生成您的数据绑定类. (重建为我带来了魔力.)Build> Rebuild
(Not Build or Make project). It will generate your data binding class. (Rebuild does Magic for me.)<data>
<variable
name="item"
type="com.package.Model"/>
</data>
binding.setItem(item);
和binding.getItem();
,但是如果看不到这些方法,请阅读以下信息.binding.setItem(item);
and binding.getItem();
, but if you can't see those methods then read the below information.<variable
,并且它未在数据绑定类中显示其设置器和获取器,则关闭并从最近的项目打开. <variable
in your layout and it does not show up its setter and getter in data binding class, then Close and Open from Recent your project. <variable
的类型,并且getter setter类型没有更改,则 Clean project(Build> Clean Project
) <variable
in your layout and getter setter type is not changing then Clean project (Build> Clean Project
)