数据绑定类未生成 [英] Data Binding class not generated

查看:104
本文介绍了数据绑定类未生成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我的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.

您应该在build.gradle中启用数据绑定.如果没有,则添加它并同步.

You should have data binding enabled in 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

Now if you want data binding class to be generated then you should wrap 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>

接着检查绑定变量名称是否正确(如视图模型类中一样)

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.

如果您的布局名称为蛇形 activity_main.xml,则为数据绑定类 将在驼峰式保护套中生成,例如ActivityMainBinding.

If your layout name is in snake case activity_main.xml then data binding class will be generated in camel case like ActivityMainBinding.

4.看不到导入建议?

有时,当您键入 ActivityMai... 时,它不显示建议,在这种情况下,手动导入.

4. Can't See Import Suggestion?

Sometimes when you type ActivityMai..., then it does not show suggestion, in that case import manually.

import <yourpackage>databinding.ActivityMainBinding;

5.阅读Build Fail Logcat

如果构建失败,则不会生成您的绑定类和布局中的新变量.因此,首先通过 Ctrl + F9(生成>创建项目) 创建项目.

  • 如果构建失败,请查看错误是什么,通常我们在布局字段中有错误.错误日志将指出出现问题的错误行号.
  • 绑定可能失败会导致一些愚蠢的错误,例如语法错误或缺少导入.在这种情况下,您将获得充满绑定类错误的logcat.但是您应该阅读完整的日志,以找到适当的问题.
  • If a build fails then see what is an error, usually we have errors in layout fields. Error logs will point out the error line number with the issue.
  • Binding may fail cause some stupid error, like syntax error or missing import. In that case, you will get logcat full of errors of binding classes. But you should read complete logcat to find appropriate issue.

我总是这样做,因为它比Rebuild/Make项目花费的时间要少得多.

I always do this because it takes much less time than Rebuild / Make project.

  • 从文件">关闭项目"中关闭项目
  • 从最近再次打开

请注意,我更喜欢最近中的关闭并打开,因为它比重建/重新启动IDE 所需的时间要短得多>.

Note that I prefer Close and Open from Recent because it takes much less time than Rebuild / Restart IDE.

如果仍然没有生成您的班级. (有时,当我们粘贴布局文件时,它会发生).然后从Build> Rebuild(不生成或创建项目)中重建项目.它将生成您的数据绑定类. (重建为我带来了魔力.)

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.)

在将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.

<data>
    <variable
        name="item"
        type="com.package.Model"/>
</data>

通常,当我们在布局中放置一个变量时,它会创建一个getter和setter方法.我们可以使用binding.setItem(item);binding.getItem();,但是如果看不到这些方法,请阅读以下信息.

Usually, when we put a variable in layout, it creates a getter and setter of it. And we can use binding.setItem(item); and binding.getItem();, but if you can't see those methods then read the below information.

如果您在布局中创建了数据变量- <variable ,并且它未在数据绑定类中显示其设置器和获取器,则关闭并从最近的项目打开.

If you have created a data 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)

If you changed the type of some <variable in your layout and getter setter type is not changing then Clean project (Build> Clean Project)

最后,如果仍然没有生成您的绑定类,那么我们拥有最强大的武器. -重新启动Android Studio:D

Finally if still your binding class is not generated, then we have our most powerful weapon. - Restart Android Studio:D

  • 首先,尝试仅重启,这总是在重启后生成我的绑定布局的变量.
  • 如果它不起作用,则 Invalidate Cache&重新启动.
  • First, try just restart, this always generates variables of my binding layout after restart.
  • If it does not work then Invalidate Cache & Restart.

这是我为解决数据绑定错误而要做的全部工作.如果您还有其他问题,可以在这里发表评论.

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

这篇关于数据绑定类未生成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆