简体   繁体   English

添加 buildFeatures { viewBinding true } 会导致“无法为 <... android:visibility> 找到接受参数类型 'int' 的 setter

[英]Adding buildFeatures { viewBinding true } results in "Cannot find a setter for <... android:visibility> that accepts parameter type 'int'

I want to start using viewBinding in our project but the mere addition of the configuration results in a compile error:我想在我们的项目中开始使用viewBinding ,但仅仅添加配置会导致编译错误:

android {
    buildFeatures {
        dataBinding true
        viewBinding true // new line and only change
    }

results in:结果是:

e: /home/leo/StudioProjects/android-wallet/mbw/build/generated/source/kapt/btctestnetDebug/com/mycelium/wallet/DataBinderMapperImpl.java:37: error: cannot find symbol
import com.mycelium.wallet.databinding.FragmentBequantAccountBindingImpl;
                                      ^
  symbol:   class FragmentBequantAccountBindingImpl
  location: package com.mycelium.wallet.databinding




Cannot find a setter for <com.mycelium.wallet.databinding.ItemBequantSearchBinding app:visibility> that accepts parameter type 'int'

If a binding adapter provides the setter, check that the adapter is annotated correctly and that the parameter type matches.

The offending code is:违规代码是:

    <data>

        <import type="android.view.View" />

        <variable
            name="viewModel"
            type="com.mycelium.bequant.market.viewmodel.AccountViewModel" />
    </data>
...
<include
    android:id="@+id/searchBar"
    layout="@layout/item_bequant_search"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:visibility="@{viewModel.searchMode ? View.VISIBLE : View.GONE}" removing="this line fixes compilation"
    app:layout_constraintTop_toBottomOf="@id/hideZeroBalance" />

Changing the offending line to any of将有问题的行更改为任何

android:visibility="@{viewModel.searchMode ? `visible` : `gone`}"
app:visibility="@{viewModel.searchMode ? View.VISIBLE : View.GONE}"

results in similar errors.导致类似的错误。

I read I might have to define a BindingAdapter but why and where?我读到我可能必须定义一个BindingAdapter但为什么以及在哪里?

I tried adding我尝试添加


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

to AccountFragment which inflates above xml file changing the xml to到 AccountFragment,它在 xml 文件上方膨胀,将 xml 更改为

android:visibility="@{viewModel.searchMode}"

but this appears to have no effect.但这似乎没有效果。

Both fragment_bequant_account.xml and item_bequant_search.xml use androidx.constraintlayout.widget.ConstraintLayout instead of androidx.constraintlayout.ConstraintLayout . fragment_bequant_account.xmlitem_bequant_search.xml使用androidx.constraintlayout.widget.ConstraintLayout而不是androidx.constraintlayout.ConstraintLayout

I tried to put a @BindingAdapter into the AccountViewModel as suggested here but with no success.我尝试按照此处的建议@BindingAdapter放入AccountViewModel中,但没有成功。

I had the same problem in my project.我在我的项目中遇到了同样的问题。 I used databinding in my code and had dataBinding true in the gradle. As soon as I added viewBinding true I got the same error pointing to the xml line android:visibility="@{viewModel.searchMode? View.VISIBLE: View.GONE}"我在我的代码中使用了数据绑定,并在 gradle 中设置了 dataBinding dataBinding true 。一旦我添加了viewBinding true ,我得到了相同的错误指向 xml 行android:visibility="@{viewModel.searchMode? View.VISIBLE: View.GONE}"

To fix , I added the tools:viewBindingIgnore="true" attribute to the root view of a certain layout file so that layout is ignored while generating binding classes.为了修复,我将tools:viewBindingIgnore="true"属性添加到某个布局文件的根视图,以便在生成绑定类时忽略布局。

You can see documentation on the tools:viewBindingIgnore="true" attribute at https://developer.android.com/topic/libraries/view-binding#data-binding您可以在https://developer.android.com/topic/libraries/view-binding#data-binding查看有关tools:viewBindingIgnore="true"属性的文档

The problem is in the viewBinding trying to create the binding class of the layout in the include.问题在于 viewBinding 试图在包含中创建布局的绑定 class。 It seems that the binding class created for the main layout(dataBinding) manages the included layout in a different way when viewBinding = true and don't understand it's attrs似乎为主布局(dataBinding)创建的绑定 class 在 viewBinding = true 时以不同的方式管理包含的布局并且不理解它的属性

As James said tools:viewBindingIgnore="true" is the solution, in this case it must be in the included layout(layout="@layout/item_bequant_search").正如 James 所说, tools:viewBindingIgnore="true"是解决方案,在这种情况下,它必须在包含的布局中(layout="@layout/item_bequant_search")。

Every reused layout must have tools:viewBindingIgnore="true" to avoid this issues每个重复使用的布局都必须有tools:viewBindingIgnore="true"来避免这个问题

The problem is with this statement问题出在这个声明上

app:visibility="@{viewModel.searchMode ? View.VISIBLE : View.GONE}"

it evaluates and pass View.VISIBLE or View.GONE to the binding adapter method,But它评估并将View.VISIBLEView.GONE传递给绑定适配器方法,但是

@BindingAdapter("visibility")
    fun setVisibility(target: View, visible: Boolean)

As your method signature says it expects a boolen but evaluation results in int ie either View.VISIBLE or View.GONE .正如您的方法签名所说,它需要一个 boolen 但评估结果为int ,即View.VISIBLEView.GONE

The issue can be solved by removing the evaluation and passing the boolean directly.去掉评估,直接传boolean即可解决。

app:visibility="@{viewModel.searchMode}"

I assument viewModel.searchMode is a boolean variable.我假设viewModel.searchMode是一个 boolean 变量。

Lets you create a kotlin file Named BindingAdapters.kt让您创建一个名为 BindingAdapters.kt 的 kotlin 文件

Paste this method directly there直接粘贴这个方法

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

else lets say you have a class BindingAdapters in a file BindingAdapters.kt否则假设您在文件 BindingAdapters.kt 中有一个 class BindingAdapters

class BindingAdapters{
     
  companion object{
    
    @BindingAdapter("visibility")
    @JvmStatic// it is important
    fun setVisibility(target: View, visible: Boolean) {
        target.visibility = if (visible) View.VISIBLE else View.GONE
    }

  }

}

I got similar error and this is my solution: You only add tag '<layout.../layout>' to all include layout like this: In main layout:我遇到了类似的错误,这是我的解决方案:您只需将标签“<layout.../layout>”添加到所有包含布局中,如下所示:在主布局中:

<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">

<data>

    <import type="android.view.View" />

    <variable
        name="viewId"
        type="Integer" />
</data>

<androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/bg">

    <include
        android:id="@+id/img_no_data"
        layout="@layout/layout_no_data"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:visibility="@{viewId==0? View.VISIBLE: View.GONE}"
        app:layout_constraintBottom_toTopOf="@+id/btn_camera"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

    
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>

In include layout: add tag '<layout...' too:在包含布局中:也添加标签 '<layout...' :

<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:layout_height="wrap_content">

   <ImageView
        android:id="@+id/imageView2"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginStart="@dimen/_16sdp"
        android:layout_marginEnd="@dimen/_16sdp"
        android:src="@drawable/bg_no_data"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintDimensionRatio="986:817"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

          
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>

Done.完毕。 Hope this help you.希望这对你有帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 AndroidX迁移-数据绑定错误msg:找不到参数类型为int的属性“ android:visibility”的设置器 - AndroidX migration - data binding error msg:Cannot find the setter for attribute 'android:visibility' with parameter type int 找不到参数类型为int的属性“ android:textColorHint”的设置器 - Cannot find the setter for attribute 'android:textColorHint' with parameter type int 找不到接受参数类型“android.text.TextWatcher”的 android.widget.EditText android:onTextChanged 的设置器 - Cannot find a setter for android.widget.EditText android:onTextChanged that accepts parameter type 'android.text.TextWatcher' 找不到接受参数类型 'kotlinx.coroutines.flow.MutableStateFlow' DataBinding Android 的设置器 - Cannot find a setter for that accepts parameter type 'kotlinx.coroutines.flow.MutableStateFlow' DataBinding Android Android 资源链接使用失败:buildFeatures {viewBinding = true} - Android Resourcse linking Failed while using : buildFeatures {viewBinding = true} 在 android.widget.ImageView 上找不到参数类型为 int 的属性“android:layout_width”的设置器 - Cannot find the setter for attribute 'android:layout_width' with parameter type int on android.widget.ImageView 找不到吸气剂<progressbar android:visibility>接受参数类型'androidx.databinding.ObservableField<java.lang.boolean> '</java.lang.boolean></progressbar> - Cannot find a getter for <ProgressBar android:visibility> that accepts parameter type 'androidx.databinding.ObservableField<java.lang.Boolean>' 找不到接受参数类型 'androidx.lifecycle.LiveData&lt; 的 ~ItemBinding~ 的设置器 - Cannot find a setter for ~ItemBinding~ that accepts parameter type 'androidx.lifecycle.LiveData< Android 数据绑定 - 找不到接受参数类型“long”的 &lt;&gt; 的 getter - Android data binding - cannot find getter for <> that accepts parameter type 'long' 如何修复 viewBinding { enabled true } 中未发生的错误 buildFeatures { viewBinding = true } - How to Fix error buildFeatures { viewBinding = true } that dont occur in viewBinding { enabled true }
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM