Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
274 views
in Technique[技术] by (71.8m points)

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:

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?

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

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.

I tried to put a @BindingAdapter into the AccountViewModel as suggested here but with no success.

question from:https://stackoverflow.com/questions/65931127/adding-buildfeatures-viewbinding-true-results-in-cannot-find-a-setter-for

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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

As your method signature says it expects a boolen but evaluation results in int i.e. either View.VISIBLE or View.GONE.

The issue can be solved by removing the evaluation and passing the boolean directly.

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

I assument viewModel.searchMode is a boolean variable.

Lets you create a kotlin file Named BindingAdapters.kt

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

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
    }

  }

}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...