无法解析 Android 数据绑定 class

Can't resolve Android databinding class

使用数据绑定时,我无法根据 Data Binding Guide

获得 class MainActivityBinding

我的布局名称是activity_main.xml。 我也看到了 但它帮不了我。

DataBinding class 将根据您的 xml 文件名生成。您正在关注的文档中明确提到了它。

By default, a Binding class will be generated based on the name of the layout file, converting it to Pascal case and suffixing “Binding” to it. The above layout file was main_activity.xml so the generate class was MainActivityBinding

如果您的 xml 名称是 activity_main.xml,则 DataBinding class 名称将是 ActivityMainBinding.

如果您的 xml 名称是 main_activity.xml,则 DataBinding class 名称将是 MainActivityBinding.

不要忘记清理和构建项目一次

您可以按照本教程了解更多 DataBinding

无法发表评论,所以我会在回答中添加这个。我相信 activity_main.xml 将创建 ActivityMainBinding 而不是您提到的 MainActivityBinding。在某些情况下,如果工作室找不到绑定 class,则只需使缓存无效并重建项目。

感谢大家 answer.I 找到了数据绑定名称为 ContentMainBinding class 的解决方案。 让我解释一下。

注意: 使用带有 <include ... 的布局时,<include layout="@layout/content_main" 具有数据绑定功能,class 与包含布局名称相关的名称。这是 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 是我添加数据绑定布局代码的布局。

所以可以用ContentMainBinding

代替MainActivityBinding

适用于我的代码如下:

//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);

完成。

默认情况下,根据布局文件的名称生成 Binding class,以大写开头,删除下划线 (_) 并将后面的字母大写,然后添加后缀“Binding”。

这个class将放在模块包下的数据绑定包中。

例如布局文件contact_item.xml会生成ContactItemBinding

如果模块包是com.example.my.app,那么它会放在com.example.my.app.databinding

绑定 class 可以通过调整数据元素的 class 属性来重命名或放置在不同的包中。例如:

<data class="ContactItem">
    ...
</data>

这会在模块包的数据绑定包中生成绑定 class 作为 ContactItem。如果 class 应该在模块包内的不同包中生成,它可能带有前缀 “.”:

<data class=".ContactItem">
    ...
</data>

这种情况下,直接在模块包中生成ContactItem。如果提供完整包,则可以使用任何包:

<data class="com.example.ContactItem">
    ...
</data>

如果 DataBinding class 名称正确 并且您曾经清理并重建项目但它仍然显示错误。
然后你尝试重启AndroidStudio

检查你activity/fragment的xml和class的名字是否一致;例如,如果你有 TimePickerFragment 而不是 xml 文件名应该是 time_picker_fragment.

我突然想到我有一个名为 fragment_time_picker 的布局;在我更改它之后生成了绑定。

在这种情况下,如果重建缓存或使缓存无效都不起作用,那么您的 xml 文件中一定有错误,您可能使用了 @{xyz.abc} 并且必须有xml.

中的 xyz 或 abc 有问题

我已经尝试了以下对我不起作用的解决方案: 1) 使缓存失效并重启。 2) 重新启动 Android Studio。 3) 重建项目。

DID 解决的问题是: 1.删​​除模块gradle.build中的"dataBinding"标签 2.同步项目 3 Return "databinding" 标签 4. 再次同步项目。

尝试将 xml 文件重命名为另一个名称并检查绑定是否有效一旦有效,将其重命名回使用的名称。

这对 Android Studio 3.1

有帮助

Binding class will be generated based on your layout file name. By default, the name of the class is based on the name of the layout file, converting it to Pascal case and adding the Binding suffix to it

例如, 如果您的 xml 名称是 activity_main.xml,那么 DataBinding class 名称将是 ActivityMainBinding

然后导入包:

import com.companyname.applicationname.databinding.ActivityMainBinding;

那你就可以用了

ActivityMainBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
User user = new User("Test", "User");
binding.setUser(user);

我已经清理了项目,重建已经完成...但是没有用。 然后使缓存无效并重新启动对我也没有帮助的项目。

然后我更改了 xml 文件名 - 效果非常好。

所以,我想告诉你一件事,请更改你的 xml 文件名。

例如:如果您的 xml 文件是:activity_main.xml 并且您无法在其 Java class 中获取 ActivityMainBinding ...... ..然后将 xml 名称更改为 main_activity.xml,然后在其 java class 中使用 MainActivityBinding 作为 'private MainActivityBinding binding;'

这很有可能。

重新启动 Android studio 或尝试将 XML 文件重命名为另一个名称并检查绑定是否有效。

尝试重新启动 Android Studio,或手动搜索 ActivityMainBinding class 并添加您的导入。

将您的数据标记放在最后包含的 xml 中。 这是一个例子:

MainActivity.java
public class MainActivity extends AppCompatActivity {

    public Item item;
    ActivityMainBinding binding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        item = new Item();
        item.setChecked(true);
        item.setName("a");
        binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
        binding.included.secondIncluded.setModel(item);


Item.java
public class Item extends BaseObservable {
    private String name;
    private Boolean checked;
    @Bindable
    public String getName() {
        return this.name;
    }
    @Bindable
    public Boolean getChecked() {
        return this.checked;
    }
    public void setName(String name) {
        this.name = name;
        notifyPropertyChanged(BR.name);
    }
    public void setChecked(Boolean checked) {
        this.checked = checked;
        notifyPropertyChanged(BR.checked);
    }
}


activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <TextView
            android:id="@+id/tv_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="abc"
            android:textSize="20sp" />
        <include
            android:id="@+id/included"
            layout="@layout/included_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </LinearLayout>
</layout>


included_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <TextView
            android:id="@+id/tv_title2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="123"
            android:textSize="20sp" />
        <include
            android:id="@+id/second_included"
            layout="@layout/second_included_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </LinearLayout>
</layout>

second_included_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">

    <data>
        <variable
            name="model"
            type="com.example.simone.twowaydatabinding.Item" />
    </data>

    <LinearLayout
        android:id="@+id/linear_layout_included"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <TextView
            android:id="@+id/tv_title1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="xyz"
            android:textSize="20sp" />
        <TextView
            android:id="@+id/tv_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@={model.name}"
            android:textSize="20sp" />
        <Switch
            android:id="@+id/switch_test"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:checked="@={model.checked}" />
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="change"
            android:onClick="button_onClick"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/world"/>
    </LinearLayout>
</layout>       

就我而言,删除应用程序构建文件夹然后重建项目解决了我的问题。

即使执行以下步骤也不起作用。

          1) Rebuild the project after adding the dataBinding  in gradle.

          2) Rebuild after adding layout tag in xml.

          3) cleaning the project

          4) rebuilding the project

          5) Restarted the Android Studio

在应用 gradle 文件中启用数据绑定后,请重建。然后我们可以发现生成了Binding class。

          If the layout name is fragment_home, The Binding class name will be FragmentHomeBinding.

我在更改源的包名后遇到了同样的问题,在我尝试了很多东西(包括这里提到的)之后,我通过手动导入数据绑定class解决了这个问题:

import com.domain.my.databinding.MyActivityBinding;

确保您的 activity_main.xml 文件包含在 layout 标签中,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<layout 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.support.constraint.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

    </android.support.constraint.ConstraintLayout>
</layout>

确保在 build.gradle 文件中添加以下行

数据绑定{ 启用真 }

我最近安装了 android studio 3.5.3 并且出现了这个问题,所以我所做的是。

1) Clean Project
2) Update Gradle (as notification was coming) 
3) Rebuild project

希望这对您有所帮助。

在我的例子中:无需重命名 XML 文件即可解决问题。

我检查了所有情况,并做了所有类似 Invalidate Caches/Restart Android Studio、clean Project、Rebuild Project 但错误没有解决的问题。

解决方案 - 我只需更改一些代码并在 activity XML 文件中再次还原该代码或更改 XML 文件中的代码。

注意 - 这是一个很晚的答案,但可能会帮助其他不想更改 XML 文件名的人。 :)

尝试查看您的 xml,如果有一个设置错误的值。这可能是关于错误的数据绑定设置

如果未声明,请将此代码放入 build.gradle(应用级别)。

android {
    dataBinding.enabled true
    viewBinding {
        enabled = true
    }
}

// Android Studio 4.0

android {
    dataBinding.enabled true
    buildFeatures {
        viewBinding = true
    }
}

我也遇到了同样的问题。但是在将 android 更新为 androidx 后解决了

尝试将 android.databinding.. 更改为 androidx.databinding...

我遇到了同样的问题,

如果您的 XML 名字是 activity_main.xml,那么 DataBinding class 名字将是 ActivityMainBinding

如果正确,请检查您是否在 xml 文件中添加了以下代码,

<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">

<data>
    <variable
        name="xyz"
        type="com.example.yourapp.classname" />
   />

</data>

如果任一方法都不是问题,请尝试

  • 清理项目
  • 重建项目

包含布局标签

<?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="wrap_content">

    </android.support.constraint.ConstraintLayout>
</layout>

您好,接受的答案已过时,新答案包含在 build.gradle(:app)

在 android 区块内

buildFeatures { viewBinding 真}

100% 有效

根据我自己的经验,无论何时包含布局,请确保根视图组具有如下所示的布局 ID。 请注意子布局文件中的 child_layout id。

父布局

        <include
            android:id="@+id/child_layout"
            layout="@layout/child_layout"/>

子布局

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:id="@+id/child_layout"
    android:layout_height="match_parent"
    android:background="#FFFFFe">
  1. 确认在 android {} 中添加以下代码 build.gradle (:app)
    buildFeatures {
       viewBinding true
    }
  1. Binding的名字就是对应的.xml文件名+“绑定”,查看.xml文件,按住 command 并单击 Activity 名称,然后跳转到那个 .xml 文件,或者您可能会得到一个下拉列表。无论哪种,都可以获得Binding。

3。如果仍然无效,请重新启动 Android Studio

仅供参考, 我正在使用 Android Studio Bumblebee | 2021.1.1

我尝试了所有基本步骤,例如

  • 清理并构建项目
  • 使缓存无效并重新启动

以上均无效。 后来自己翻了Gradle文件才找到的

android 标签内的 module/build.gradle 文件中添加以下代码。

kotlinOptions {
    jvmTarget = '1.8'
}

module/build.gradle 中的这一行解决了我的问题:

buildFeatures {
    viewBinding true
}