无法解析 Android 数据绑定类

新手上路,请多包涵
阅读 573
2 个回答

感谢大家的回答。我找到了 ContentMainBinding 数据绑定类名的解决方案。让我解释一下。

注意: 当使用带有 <include ... 的布局时,这里是 <include layout="@layout/content_main" 具有数据绑定功能,与包含布局名称相关的类名。这是 ContentMainBinding

我的布局文件如下:

activity_main.xml

 <?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context="com.databindingdemo.app.MainActivity">
    ...
    <include layout="@layout/content_main" />
    ...
    </android.support.design.widget.CoordinatorLayout>

content_main.xml 是我添加数据绑定布局代码的布局。

因此,与其使用 MainActivityBinding 它可以用 ContentMainBinding 解决

对我有用的代码如下:

 //Code for data binding
    ContentMainBinding contentMainBinding = DataBindingUtil.setContentView(this, R.layout.content_main);
    user = new User("Pranay", "Patel", "demoemail@gmail.com", "9999999999");
    contentMainBinding.setUser(user);

完毕。

原文由 pRaNaY 发布,翻译遵循 CC BY-SA 3.0 许可协议

module/build.gradle 里面的这一行为我解决了这个问题:

 buildFeatures {
    viewBinding true
}

原文由 Jonathan Applebaum 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题