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

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

问题描述

我在我的项目中使用数据绑定,当在我的 xml 绑定类中使用 时没有生成.

例如我有activity_main.xml

</RelativeLayout></布局>

现在,如果我在我的活动/片段中编写 ActivityMainBinding,它会显示类不可用的错误.但是在我的 xml 文件中包含 之后,它能够生成 ActivityMainBinding 类.

Android Studio:2.1.3
类路径:com.android.tools.build:gradle:2.1.3
minSdkVersion 16
targetSdkVersion 24
buildToolsVersion 24.0.0

解决方案

我没有得到任何令人满意的答案.所以这里的提示是我对数据绑定知识的总结.

解决数据绑定问题的技巧

更新

要获得更准确的错误建议,我强烈建议将 Android Studio 和 Gradle 插件版本更新到最新版本.因为我在 AS 3.2 版本之后没有遇到很多问题.

请参阅最新 Android Studio最新的 Gradle 插件.

原始解决方案

<块引用>

阅读此答案后,您不会陷入数据绑定自动生成问题对于类和数据变量.

这些点一一核对.这些中的任何一个都可以完成您的工作.最后的第 3 点非常重要,所以不要错过它们.

1.检查是否启用数据绑定

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

android {...构建功能{数据绑定真}}

2.检查布局转换为绑定布局

现在如果你想要生成数据绑定类,那么你应该用数据绑定包装xml layout(<layout 标签).像这样的东西.

<layout xmlns:android="http://schemas.android.com/apk/res/android"><android.support.constraint.ConstraintLayoutandroid:layout_width=match_parent"android:layout_height=match_parent"></android.support.constraint.ConstraintLayout></布局>

同时检查绑定变量名称是否与视图模型类中的名称相同

3.自动生成的绑定类名?

您的数据绑定类应在创建绑定布局后生成.

<块引用>

如果您的布局名称是 snake case activity_main.xml 那么数据绑定类将在 camel case 中生成,如 ActivityMainBinding.

4.看不到导入建议?

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

import databinding.ActivityMainBinding;

5.阅读构建失败日志

如果构建失败,将不会生成布局中的绑定类和新变量.所以首先制作项目通过Ctrl + F9(构建>制作项目).

  • 如果构建失败,那么查看什么是错误,通常我们在布局字段中有错误.错误日志会指出问题所在的错误行号.
  • 绑定可能失败导致一些愚蠢的错误,例如语法错误或缺少导入.在这种情况下,您将得到充满绑定类错误的 logcat.但是您应该阅读完整的 logcat找到合适的问题.

6.关闭并打开最近的项目

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

  • 从文件中关闭项目>关闭项目
  • 从最近重新打开

请注意我更喜欢最近中的关闭和打开,因为它比重新构建/重新启动 IDE 花费的时间少得多>.

7.重建项目

如果仍然没有生成您的类.(有时当我们粘贴布局文件时,它就会发生).然后从Build>Rebuild Project;重建(不构建或制作项目).它将生成您的数据绑定类.(重建对我来说很神奇.)

8.拥有最新的 Android Studio

更新AS到Android Studio 3.2后,感觉数据绑定自动修复了很多bug一代.因此,您还应该拥有最新的 AS.

#<变量

的解决方案

<变量名称=项目"type=com.package.Model"/></数据>

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

1.关闭并打开最近的项目

如果您在布局中创建了数据变量 - 并且它没有在数据中显示其 setter 和 getter绑定类,然后关闭并从最近打开您的项目.

2.更改类型后清理项目

如果您更改了布局中某些 的类型并且 getter setter 类型未更改,则 Clean 项目(Build> Clean项目)

最后的话

最后,如果你的绑定类仍然没有生成,那么我们就有了我们最强大的武器.- 重启 Android Studio

  • 首先,尝试重启,这总是在重启后生成我的绑定布局的变量.
  • 如果它不起作用,则使缓存无效&重新启动.

这就是我为解决数据绑定错误所做的一切.如果您有任何进一步的问题,您可以在这里评论.

I am using Data Binding in my project, when using <layout> and <data> in my xml binding class is not generated.

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>

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

Tips For Solving DataBinding Issues

Update

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.

See Latest Android Studio, and Latest Gradle Plugin.

Orignal Solution

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.

1. Check if data-binding enabled

You should have data binding enabled in build.gradle. If not then add this and Sync.

android {
    ...
    buildFeatures {
        dataBinding true
    }
}

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

3. Auto-generated Binding class name?

Your data binding class should be generated after creating binding layout.

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

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. Read Build Fail Logcat

Your binding class and new variables in layout will not be generated if your build fails. So first Make project by Ctrl + F9 (Build > Make project).

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

6. Close and open project from recent

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

  • Close project from File > Close Project
  • Open again from recent

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

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

8. Have latest Android Studio

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.

#Solution for <variables

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

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.

1. Close and open project from recent

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.

2. Clean project after changing the type

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

Final words

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

  • 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天全站免登陆