【问题标题】:Android BindingAdapter error.NonExistentClassAndroid BindingAdapter 错误。NonExistentClass
【发布时间】:2017-07-10 11:00:28
【问题描述】:

这是我的 BindingAdapter

@InverseBindingMethods({
        @InverseBindingMethod(type = RecyclerView.class, attribute = "lastAdapter", method = "getLastAdapter"),
})
public class RecyclerViewBindingAdapter {
    @BindingAdapter(value = {"lastAdapter"}, requireAll = false)
    public static void setLastAdapter(RecyclerView view, LastAdapter adapter) {
        view.setAdapter(adapter);
    }

    @InverseBindingAdapter(attribute = "lastAdapter")
    public static LastAdapter getLastAdapter(RecyclerView view) {
        return (LastAdapter) view.getAdapter();
    }
}

我在我的 gradle 上添加了 RecyclerView 依赖项:

compile "com.android.support:recyclerview-v7:25.3.1"

这是我在布局中的使用方式:

<android.support.v7.widget.RecyclerView
 app:lastAdapter = "@{viewModel.adapter}"
 ... />

我已经在扩展此布局后设置了 viewModel 绑定变量。但是每当我运行我的代码时,gradle 总是显示以下错误

Error:(48, 34) Cannot find the setter for attribute 'app:lastAdapter' with parameter type error.NonExistentClass on android.support.v7.widget.RecyclerView. 

我可以通过从我的布局中删除任何app:lastAdapter 然后清理并重建,再次重写那些app:lastAdapter 属性然后重建项目来解决这个问题,一切正常。

这不是一个方便的解决方案。

【问题讨论】:

  • 你的变量行是什么样的?你的 ViewModel 中的 adapter 是什么?
  • 嗨 George,adapter 是来自 LastAdapter 类的对象,这里是 var adapter = LastAdapter(data, BR.item).map&lt;MenuLinked, MenuItemBinding&gt;(R.layout.menu_item)
  • 您的 BindingAdapter 采用 LastAdapter,但适配器似乎是不同的类型。我读对了吗?
  • 看看adapter被隐式声明为LastAdapter。可能是因为类型没有明确声明吗?让我试试
  • 谢谢乔治。就是这样,由于Kotlin隐式类型赋值,BindingAdapter无法确定类型

标签: android android-databinding android-binding-adapter


【解决方案1】:

正如 George 在上面的评论中所说的 “BindingAdapter 采用 LastAdapter,但适配器似乎是不同的类型” 这就是变量的分配方式:

var adapter = LastAdapter(data, BR.item).map<MenuLinked, MenuItemBinding>(R.layout.menu_item)

DataBinding 库似乎无法从 Kotlin 中确定隐式数据类型声明,因此需要显式定义:

var adapter : LastAdapter = LastAdapter(data, BR.item).map<MenuLinked, MenuItemBinding>(R.layout.menu_item)

【讨论】:

    【解决方案2】:

    在 android studio 终端中运行此命令并再次构建项目

    ./gradlew clean assemble

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-03-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多