自定义 View 的 Android 数据绑定(bind) : 'cannot find symbol' and 'duplicate class found'

标签 android android-databinding

我之前更改了项目中的一些代码,现在在编译以下自动生成的类时抛出以下错误:

DataBinderMapperImpl.java:找不到符号“AdvancedBoxBindingImpl”(在“导入 ...databinding.AdvancedBoxBindingImpl;”行中)

AdvancedBox 包含在布局 fragment_config.xml 中,如下所示:

<layout xmlns:bind="http://schemas.android.com/tools">
    <data>
        <import type="android.view.View"/>
        <variable name="viewModel" type ="...GlobalConfigViewModel"/>
    </data>
...
<....AdvancedBox
                android:id="@+id/advancedBox"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:viewModel = "@{viewModel}"
                android:visibility="@{viewModel.advancedShown ? View.VISIBLE : View.GONE}"/>
...
</layout>

advanced_box.xml 如下所示:

<layout>
    <data>
        <import type="android.view.View"/>
        <variable
            name="viewModel"
            type="...GlobalConfigViewModel" />
    </data>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    ....

AdvancedBox类中的绑定(bind)是这样实例化的:

class AdvancedBox : LinearLayout{
    private lateinit var mBinding: AdvancedBoxBinding
    init{
        mBinding = AdvancedBoxBinding.inflate(inflater, this, true)
    }

所有这些以前都有效,但我最近更新了 Android Studio 并对代码进行了更改。

附加信息:

  • gradle.properties 具有属性 'android.databinding.enableV2=true'
  • 我试过文件->使缓存无效并重新启动

对可能出现的问题有什么建议吗?

最佳答案

数据绑定(bind)到 View 的方式可能存在问题。 就我而言,我有一个模型:

<data>
    <variable name="user" type="zw.org.coop.models.User"/>
</data>

但由于复制粘贴,我有一个错字:

<TextView
    android:id="@+id/fullName"
    style="@style/styleEditText"
    android:layout_width="match_parent"
    android:layout_height="35dp"
    android:padding="5dp"
    android:text="@{user.user.firstName}"
    android:textSize="12sp"
    tools:text="Dean Kaila" />

user.user.firstName 不是预期的,而是 user.firstName

如果 Jetbrains 有一些 intellisense 继续帮助解决此类问题,那会更好,但是,是的,这对我来说是美好的 2 天。 希望这对某人有帮助。

关于自定义 View 的 Android 数据绑定(bind) : 'cannot find symbol' and 'duplicate class found' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53460833/

相关文章:

android - 无法在 gradle 文件中应用 'android-reporting'

android - 使用 GSON 库从 URL(RESTful web 服务)解析 JSON 字符串。安卓

向 Gcm 发送 1000 多个用户的 Php 函数

java - 使用数据绑定(bind)时,不会调用自定义 setter

android - 是否可以将参数发送到来自 xml View 的绑定(bind)事件

android - ADB 在运行 Android 应用程序时不断断开连接

java - 如何以编程方式在中心裁剪特定尺寸的位图

android - 同一个项目中的 Realm.io/Dagger/Databinding

android - Kotlin 和 Android 数据绑定(bind) (BuildTools 2.0.0)