androidandroid-imageviewvisibilityandroid-databindingandroid-binding-adapter

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


I have a little problem with databindig in Android. I want to set an ImageView's visibility through databindig, and I think I have done everything that can be found on Android blogs in connection with databinding, though I get that build error message.

My layout XML file is the following:

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <data>
        <variable
            name="mapViewModel"
            type="neptun.jxy1vz.cluedo.ui.map.MapViewModel" />
    </data>

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <HorizontalScrollView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent">

            <androidx.constraintlayout.widget.ConstraintLayout
                android:id="@+id/mapLayout"
                android:layout_width="wrap_content"
                android:layout_height="match_parent">

                <ImageView
                    android:id="@+id/ivMap"
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:src="@drawable/map"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toTopOf="parent"
                    android:contentDescription="@string/map_description" />

                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:id="@+id/ivDoor"
                    android:src="@drawable/door_left"
                    app:layout_constraintStart_toStartOf="@id/guidelineCol7"
                    app:layout_constraintTop_toTopOf="@id/guidelineRow1"
                    android:visibility="@={mapViewModel.doorVisibility}"/>

                    <!-- Guidelines... they are not interesting -->

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

And my ViewModel class:

package neptun.jxy1vz.cluedo.ui.map

import android.view.View
import androidx.databinding.BaseObservable
import androidx.databinding.Bindable
import androidx.databinding.BindingAdapter
import androidx.databinding.library.baseAdapters.BR
import java.util.*

class MapViewModel() : BaseObservable() {

    @Bindable private var doorVisibility = View.VISIBLE

    fun getDoorVisibility(): Int { return doorVisibility }

    @BindingAdapter("android:visibility")
    fun setDoorVisibility(visibility: Int) {
        doorVisibility = visibility
        notifyPropertyChanged(BR.doorVisibility)
    }
}

I cannot find out what's the problem...

The complete error log:

e: P:\Dokumentumok\Suli\BME-VIK_6\Cluedo\Application\app\build\generated\source\kapt\debug\neptun\jxy1vz\cluedo\DataBinderMapperImpl.java:18: error: cannot find symbol
import neptun.jxy1vz.cluedo.databinding.ActivityMapBindingImpl;
                                       ^
  symbol:   class ActivityMapBindingImpl
  location: package neptun.jxy1vz.cluedo.databinding
e: P:\Dokumentumok\Suli\BME-VIK_6\Cluedo\Application\app\build\tmp\kapt3\stubs\debug\neptun\jxy1vz\cluedo\ui\map\MapViewModel.java:100: error: @BindingAdapter setDoor0Visibility(int) has 1 attributes and 0 value parameters. There should be 1 or 2 value parameters.
    public final void setDoor0Visibility(int visibility) {
                      ^
e: P:\Dokumentumok\Suli\BME-VIK_6\Cluedo\Application\app\build\tmp\kapt3\stubs\debug\neptun\jxy1vz\cluedo\ui\map\MapViewModel.java:104: error: @BindingAdapter setDoor2Visibility(int) has 1 attributes and 0 value parameters. There should be 1 or 2 value parameters.
    public final void setDoor2Visibility(int visibility) {
                      ^
e: P:\Dokumentumok\Suli\BME-VIK_6\Cluedo\Application\app\build\tmp\kapt3\stubs\debug\neptun\jxy1vz\cluedo\ui\map\MapViewModel.java:108: error: @BindingAdapter setDoor4Visibility(int) has 1 attributes and 0 value parameters. There should be 1 or 2 value parameters.
    public final void setDoor4Visibility(int visibility) {
                      ^
e: P:\Dokumentumok\Suli\BME-VIK_6\Cluedo\Application\app\build\tmp\kapt3\stubs\debug\neptun\jxy1vz\cluedo\ui\map\MapViewModel.java:112: error: @BindingAdapter setDoor6Visibility(int) has 1 attributes and 0 value parameters. There should be 1 or 2 value parameters.
    public final void setDoor6Visibility(int visibility) {
                      ^
e: P:\Dokumentumok\Suli\BME-VIK_6\Cluedo\Application\app\build\tmp\kapt3\stubs\debug\neptun\jxy1vz\cluedo\ui\map\MapViewModel.java:116: error: @BindingAdapter setDoor7Visibility(int) has 1 attributes and 0 value parameters. There should be 1 or 2 value parameters.
    public final void setDoor7Visibility(int visibility) {
                      ^
e: P:\Dokumentumok\Suli\BME-VIK_6\Cluedo\Application\app\build\tmp\kapt3\stubs\debug\neptun\jxy1vz\cluedo\ui\map\MapViewModel.java:120: error: @BindingAdapter setDoor12Visibility(int) has 1 attributes and 0 value parameters. There should be 1 or 2 value parameters.
    public final void setDoor12Visibility(int visibility) {
                      ^
e: P:\Dokumentumok\Suli\BME-VIK_6\Cluedo\Application\app\build\tmp\kapt3\stubs\debug\neptun\jxy1vz\cluedo\ui\map\MapViewModel.java:124: error: @BindingAdapter setDoor13Visibility(int) has 1 attributes and 0 value parameters. There should be 1 or 2 value parameters.
    public final void setDoor13Visibility(int visibility) {
                      ^
e: P:\Dokumentumok\Suli\BME-VIK_6\Cluedo\Application\app\build\tmp\kapt3\stubs\debug\neptun\jxy1vz\cluedo\ui\map\MapViewModel.java:128: error: @BindingAdapter setDoor15Visibility(int) has 1 attributes and 0 value parameters. There should be 1 or 2 value parameters.
    public final void setDoor15Visibility(int visibility) {
                      ^
e: P:\Dokumentumok\Suli\BME-VIK_6\Cluedo\Application\app\build\tmp\kapt3\stubs\debug\neptun\jxy1vz\cluedo\ui\map\MapViewModel.java:132: error: @BindingAdapter setDoor17Visibility(int) has 1 attributes and 0 value parameters. There should be 1 or 2 value parameters.
    public final void setDoor17Visibility(int visibility) {
                      ^
e: P:\Dokumentumok\Suli\BME-VIK_6\Cluedo\Application\app\build\tmp\kapt3\stubs\debug\neptun\jxy1vz\cluedo\ui\map\MapViewModel.java:136: error: @BindingAdapter setDoor19Visibility(int) has 1 attributes and 0 value parameters. There should be 1 or 2 value parameters.
    public final void setDoor19Visibility(int visibility) {
                      ^
e: P:\Dokumentumok\Suli\BME-VIK_6\Cluedo\Application\app\build\tmp\kapt3\stubs\debug\neptun\jxy1vz\cluedo\ui\map\MapViewModel.java:140: error: @BindingAdapter setDoor20Visibility(int) has 1 attributes and 0 value parameters. There should be 1 or 2 value parameters.
    public final void setDoor20Visibility(int visibility) {
                      ^
e: P:\Dokumentumok\Suli\BME-VIK_6\Cluedo\Application\app\build\tmp\kapt3\stubs\debug\neptun\jxy1vz\cluedo\ui\map\MapViewModel.java:144: error: @BindingAdapter setDoor21Visibility(int) has 1 attributes and 0 value parameters. There should be 1 or 2 value parameters.
    public final void setDoor21Visibility(int visibility) {
                      ^
w: P:\Dokumentumok\Suli\BME-VIK_6\Cluedo\Application\app\build\tmp\kapt3\stubs\debug\neptun\jxy1vz\cluedo\ui\map\MapViewModel.java:189: warning: Application namespace for attribute app:layout_constraintTop_toTopOf will be ignored.
    public final void setLayoutConstraintTop(@org.jetbrains.annotations.NotNull()
                      ^
w: P:\Dokumentumok\Suli\BME-VIK_6\Cluedo\Application\app\build\tmp\kapt3\stubs\debug\neptun\jxy1vz\cluedo\ui\map\MapViewModel.java:194: warning: Application namespace for attribute app:layout_constraintStart_toStartOf will be ignored.
    public final void setLayoutConstraintStart(@org.jetbrains.annotations.NotNull()

e: [kapt] An exception occurred: android.databinding.tool.util.LoggedErrorException: Found data binding error(s):

[databinding] {"msg":"Cannot find a getter for \u003candroid.widget.ImageView android:visibility\u003e that accepts parameter type \u0027int\u0027\n\nIf a binding adapter provides the getter, check that the adapter is annotated correctly and that the parameter type matches.","file":"P:\\Dokumentumok\\Suli\\BME-VIK_6\\Cluedo\\Application\\app\\src\\main\\res\\layout\\activity_map.xml","pos":[{"line0":174,"col0":16,"line1":181,"col1":74}]}

    at android.databinding.tool.processing.Scope.assertNoError(Scope.java:111)
    at android.databinding.annotationprocessor.ProcessDataBinding.doProcess(ProcessDataBinding.java:124)
    at android.databinding.annotationprocessor.ProcessDataBinding.process(ProcessDataBinding.java:88)
    at org.jetbrains.kotlin.kapt3.base.incremental.IncrementalProcessor.process(incrementalProcessors.kt)
    at org.jetbrains.kotlin.kapt3.base.ProcessorWrapper.process(annotationProcessing.kt:147)
    at com.sun.tools.javac.processing.JavacProcessingEnvironment.callProcessor(JavacProcessingEnvironment.java:794)
    at com.sun.tools.javac.processing.JavacProcessingEnvironment.access$200(JavacProcessingEnvironment.java:91)
    at com.sun.tools.javac.processing.JavacProcessingEnvironment$DiscoveredProcessors$ProcessorStateIterator.runContributingProcs(JavacProcessingEnvironment.java:627)
    at com.sun.tools.javac.processing.JavacProcessingEnvironment$Round.run(JavacProcessingEnvironment.java:1033)
    at com.sun.tools.javac.processing.JavacProcessingEnvironment.doProcessing(JavacProcessingEnvironment.java:1198)
    at com.sun.tools.javac.main.JavaCompiler.processAnnotations(JavaCompiler.java:1170)
    at com.sun.tools.javac.main.JavaCompiler.processAnnotations(JavaCompiler.java:1068)
    at org.jetbrains.kotlin.kapt3.base.AnnotationProcessingKt.doAnnotationProcessing(annotationProcessing.kt:79)
    at org.jetbrains.kotlin.kapt3.base.AnnotationProcessingKt.doAnnotationProcessing$default(annotationProcessing.kt:35)
    at org.jetbrains.kotlin.kapt3.AbstractKapt3Extension.runAnnotationProcessing(Kapt3Extension.kt:224)
    at org.jetbrains.kotlin.kapt3.AbstractKapt3Extension.analysisCompleted(Kapt3Extension.kt:187)
    at org.jetbrains.kotlin.kapt3.ClasspathBasedKapt3Extension.analysisCompleted(Kapt3Extension.kt:98)
    at org.jetbrains.kotlin.cli.jvm.compiler.TopDownAnalyzerFacadeForJVM$analyzeFilesWithJavaIntegration$2.invoke(TopDownAnalyzerFacadeForJVM.kt:97)
    at org.jetbrains.kotlin.cli.jvm.compiler.TopDownAnalyzerFacadeForJVM.analyzeFilesWithJavaIntegration(TopDownAnalyzerFacadeForJVM.kt:107)
    at org.jetbrains.kotlin.cli.jvm.compiler.TopDownAnalyzerFacadeForJVM.analyzeFilesWithJavaIntegration$default(TopDownAnalyzerFacadeForJVM.kt:82)
    at org.jetbrains.kotlin.cli.jvm.compiler.KotlinToJVMBytecodeCompiler$analyze$1.invoke(KotlinToJVMBytecodeCompiler.kt:557)
    at org.jetbrains.kotlin.cli.jvm.compiler.KotlinToJVMBytecodeCompiler$analyze$1.invoke(KotlinToJVMBytecodeCompiler.kt:82)
    at org.jetbrains.kotlin.cli.common.messages.AnalyzerWithCompilerReport.analyzeAndReport(AnalyzerWithCompilerReport.kt:107)
    at org.jetbrains.kotlin.cli.jvm.compiler.KotlinToJVMBytecodeCompiler.analyze(KotlinToJVMBytecodeCompiler.kt:548)
    at org.jetbrains.kotlin.cli.jvm.compiler.KotlinToJVMBytecodeCompiler.compileModules$cli(KotlinToJVMBytecodeCompiler.kt:177)
    at org.jetbrains.kotlin.cli.jvm.K2JVMCompiler.doExecute(K2JVMCompiler.kt:165)
    at org.jetbrains.kotlin.cli.jvm.K2JVMCompiler.doExecute(K2JVMCompiler.kt:55)
    at org.jetbrains.kotlin.cli.common.CLICompiler.execImpl(CLICompiler.kt:84)
    at org.jetbrains.kotlin.cli.common.CLICompiler.execImpl(CLICompiler.kt:42)
    at org.jetbrains.kotlin.cli.common.CLITool.exec(CLITool.kt:104)
    at org.jetbrains.kotlin.daemon.CompileServiceImpl.compile(CompileServiceImpl.kt:1558)
    at sun.reflect.GeneratedMethodAccessor132.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:357)
    at sun.rmi.transport.Transport$1.run(Transport.java:200)
    at sun.rmi.transport.Transport$1.run(Transport.java:197)
    at java.security.AccessController.doPrivileged(Native Method)
    at sun.rmi.transport.Transport.serviceCall(Transport.java:196)
    at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:573)
    at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:834)
    at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.lambda$run$0(TCPTransport.java:688)
    at java.security.AccessController.doPrivileged(Native Method)
    at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:687)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at java.lang.Thread.run(Thread.java:748)


FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:kaptDebugKotlin'.
> Compilation error. See log for more details

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

* Get more help at https://help.gradle.org

BUILD FAILED in 4s

Solution

  • @BindingAdapter("visible")
    fun bindVisible(imageView: ImageView, visible: Boolean) {
       set visibility according to the boolean
    }
    

    in your xml write

    app:visible="@{viewModel.visible}
    

    In your code file have a observable boolean

    val visible = ObservableBoolean()