【问题标题】:Cannot find a getter for <ProgressBar android:visibility> that accepts parameter type 'androidx.databinding.ObservableField<java.lang.Boolean>'找不到接受参数类型“androidx.databinding.ObservableField<java.lang.Boolean>”的 <ProgressBar android:visibility> 的 getter
【发布时间】:2021-03-10 20:10:23
【问题描述】:

其他问题已通过将kotlin-kapt 插件添加到gradle 文件中得到解决,但我的问题似乎并没有就此消失。我正在尝试设置进度条的可见性,以便用户知道应用程序何时在后台运行。我为此使用数据绑定,但出现编译错误:

找不到接受参数类型'androidx.databinding.ObservableField'的getter

我认为字符串或整数值在那里也不起作用。至少对于 boolean 方法,它不会抱怨 setter,只会抱怨 getter。尽管我将数据绑定与其他视图一起使用并且效果很好。 这就是我目前在 HomeViewModel 中实现我的解决方案的方式:

 @Bindable
    var barProgress = ObservableField<Boolean>()
    fun doSomething(){
        this.barProgress.set(true)
    }

    @BindingAdapter("android:visibility")
    fun setVisibility(view: View, visible: Boolean) {
        view.visibility = if (visible) View.INVISIBLE else View.VISIBLE
    }

在布局文件中:

<ProgressBar
            android:id="@+id/progressBar"
            style="?android:attr/progressBarStyle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="15dp"
            android:layout_marginLeft="15dp"
            app:layout_constraintStart_toEndOf="@+id/tag_button"
            app:layout_constraintTop_toBottomOf="@+id/editTextTextMultiLine"
            android:visibility="@={myViewModel.barProgress}" //--> this is where it complains />

所以在我的 HomeViewModel 中,我应该能够将 barProgress 设置为 true 或 false。

我的build.gradle

plugins {
    id 'com.android.application'
    id 'kotlin-android'
    id 'kotlin-kapt'
}

知道我应该怎么做吗?

【问题讨论】:

    标签: android kotlin view build.gradle kapt


    【解决方案1】:

    它只是清理和重建项目,我也不知道它是否相关但我替换了:

    plugins {
        id 'com.android.application'
        id 'kotlin-android'
        id 'kotlin-kapt'
    }
    

    与:

    apply plugin: 'com.android.application'
    apply plugin: 'kotlin-android'
    apply plugin: 'kotlin-kapt'
    

    不确定这是否也有助于解决问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-06-02
      • 2023-03-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多