找不到属性 app:visibleGone 的 setter
Cannot find setter for attribute app:visibleGone
我正在尝试在我的 android 应用程序中实现 MVVM 架构。我也在使用 Kotlin。
这是我的绑定适配器class:
class BindingAdapter {
companion object {
@JvmStatic @BindingAdapter("app:visibleGone") fun showHide(view: View, show: Boolean) {
view.visibility =
if (show)
View.VISIBLE
else
View.GONE
}
}
}
这是我的 XML 文件:
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
<variable
name="isLoading"
type="boolean"/>
</data>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/cardview_light_background"
android:orientation="vertical">
<TextView
android:id="@+id/loading_rates"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical|center_horizontal"
android:text="@string/loading_rates"
android:textAlignment="center"
app:visibleGone="@{isLoading}"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/cardview_light_background"
android:orientation="vertical"
app:visibleGone="@{!isLoading}">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:layout_marginTop="20dp"
android:gravity="center_horizontal"
android:text="@string/rate_list"
android:textAlignment="center"
android:textSize="@dimen/rate_text"
android:textStyle="bold"/>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rate_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layoutManager="LinearLayoutManager"/>
</LinearLayout>
</LinearLayout>
</layout>
错误消息说:
Found data binding errors.
****/ data binding error ****msg:Cannot find the setter for attribute 'app:visibleGone' with parameter type boolean on android.widget.TextView. file:/home/sparker0i/CurrencyConverter/app/src/main/res/layout/fragment_rate_list.xml loc:23:35 - 23:43 ****\ data binding error ****
第 23 行解析为 loading_rates TextView 的 app:visibleGone 语句正上方的行
我无法理解尽管在我的 Kotlin 中设置了 BindingAdapter class,但为什么我无法成功编译代码?
您能否尝试在 BindingAdapter
注释中去掉 app:
前缀并更改您的第一个参数类型:
@JvmStatic
@BindingAdapter("visibleGone")
fun showHide(view: TextView, show: Boolean) {
view.visibility = if (show) View.VISIBLE else View.GONE
}
您可以尝试隐藏和显示 xml 本身的视图,您不需要单独的绑定适配器。我希望这能解决您的问题。
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
<import type="android.view.View" />
<variable
name="isLoading"
type="boolean"/>
</data>
<TextView
android:id="@+id/loading_rates"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical|center_horizontal"
android:text="@string/loading_rates"
android:textAlignment="center"
android:visibility="@{isLoading?View.VISIBLE:View.GONE}"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/cardview_light_background"
android:orientation="vertical"
android:visibility="@{isLoading?View.GONE:View.VISIBLE}">
<layout>
要解决该问题,请检查以下事项:
在根 build.gradle 你有
buildscript {
ext.android_plugin_version = '3.1.2'
dependencies {
classpath "com.android.tools.build:gradle:$android_plugin_version"
}
}
在app/build.gradle
apply plugin: 'kotlin-kapt'
dependencies {
...
kapt "com.android.databinding:compiler:$android_plugin_version"
}
有了所有这些东西,问题应该就消失了,您可以为基数 class 编写 @BindingAdapter
并将它们应用于该基数 class.
的子代
在您尝试任何复杂的解决方案之前,只需确认您已在您的应用程序中应用以下插件 app.gradle
apply plugin: 'kotlin-kapt'
如果缺少那个,它会一直报错。经过 8 小时的争吵和阅读其他 SOF 帖子后,我发现我的那个不见了。
成功了。
添加图片以供参考:
我知道这里已经有几个答案,它们都适用于不同的情况。
但我最近遇到了这个问题,我花了一些时间才弄清楚你必须做的前两件事是:
- 检查您的模块 gradle 文件中是否有
apply plugin: 'kotlin-kapt'
;
- 检查您的模块 gradle 文件
android {...}
块中是否有 dataBinding { enabled = true }
。
对我来说,问题在第 2 点。
我正在尝试在我的 android 应用程序中实现 MVVM 架构。我也在使用 Kotlin。
这是我的绑定适配器class:
class BindingAdapter {
companion object {
@JvmStatic @BindingAdapter("app:visibleGone") fun showHide(view: View, show: Boolean) {
view.visibility =
if (show)
View.VISIBLE
else
View.GONE
}
}
}
这是我的 XML 文件:
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
<variable
name="isLoading"
type="boolean"/>
</data>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/cardview_light_background"
android:orientation="vertical">
<TextView
android:id="@+id/loading_rates"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical|center_horizontal"
android:text="@string/loading_rates"
android:textAlignment="center"
app:visibleGone="@{isLoading}"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/cardview_light_background"
android:orientation="vertical"
app:visibleGone="@{!isLoading}">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:layout_marginTop="20dp"
android:gravity="center_horizontal"
android:text="@string/rate_list"
android:textAlignment="center"
android:textSize="@dimen/rate_text"
android:textStyle="bold"/>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rate_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layoutManager="LinearLayoutManager"/>
</LinearLayout>
</LinearLayout>
</layout>
错误消息说:
Found data binding errors.
****/ data binding error ****msg:Cannot find the setter for attribute 'app:visibleGone' with parameter type boolean on android.widget.TextView. file:/home/sparker0i/CurrencyConverter/app/src/main/res/layout/fragment_rate_list.xml loc:23:35 - 23:43 ****\ data binding error ****
第 23 行解析为 loading_rates TextView 的 app:visibleGone 语句正上方的行
我无法理解尽管在我的 Kotlin 中设置了 BindingAdapter class,但为什么我无法成功编译代码?
您能否尝试在 BindingAdapter
注释中去掉 app:
前缀并更改您的第一个参数类型:
@JvmStatic
@BindingAdapter("visibleGone")
fun showHide(view: TextView, show: Boolean) {
view.visibility = if (show) View.VISIBLE else View.GONE
}
您可以尝试隐藏和显示 xml 本身的视图,您不需要单独的绑定适配器。我希望这能解决您的问题。
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
<import type="android.view.View" />
<variable
name="isLoading"
type="boolean"/>
</data>
<TextView
android:id="@+id/loading_rates"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical|center_horizontal"
android:text="@string/loading_rates"
android:textAlignment="center"
android:visibility="@{isLoading?View.VISIBLE:View.GONE}"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/cardview_light_background"
android:orientation="vertical"
android:visibility="@{isLoading?View.GONE:View.VISIBLE}">
<layout>
要解决该问题,请检查以下事项:
在根 build.gradle 你有
buildscript {
ext.android_plugin_version = '3.1.2'
dependencies {
classpath "com.android.tools.build:gradle:$android_plugin_version"
}
}
在app/build.gradle
apply plugin: 'kotlin-kapt'
dependencies {
...
kapt "com.android.databinding:compiler:$android_plugin_version"
}
有了所有这些东西,问题应该就消失了,您可以为基数 class 编写 @BindingAdapter
并将它们应用于该基数 class.
在您尝试任何复杂的解决方案之前,只需确认您已在您的应用程序中应用以下插件 app.gradle
apply plugin: 'kotlin-kapt'
如果缺少那个,它会一直报错。经过 8 小时的争吵和阅读其他 SOF 帖子后,我发现我的那个不见了。 成功了。
添加图片以供参考:
我知道这里已经有几个答案,它们都适用于不同的情况。 但我最近遇到了这个问题,我花了一些时间才弄清楚你必须做的前两件事是:
- 检查您的模块 gradle 文件中是否有
apply plugin: 'kotlin-kapt'
; - 检查您的模块 gradle 文件
android {...}
块中是否有dataBinding { enabled = true }
。
对我来说,问题在第 2 点。