如果绑定适配器提供 getter,请检查适配器是不是正确注释以及参数类型是不是匹配
Posted
技术标签:
【中文标题】如果绑定适配器提供 getter,请检查适配器是不是正确注释以及参数类型是不是匹配【英文标题】:If a binding adapter provides the getter, check that the adapter is annotated correctly and that the parameter type matches如果绑定适配器提供 getter,请检查适配器是否正确注释以及参数类型是否匹配 【发布时间】:2020-07-22 16:02:27 【问题描述】:我对 android 中的 databindig 有一点问题。 我想通过 databindig 设置 ImageView 的可见性,我想我已经完成了所有可以在 Android 博客上找到的与数据绑定相关的事情,尽管我收到了构建错误消息。
我的布局 XML 文件如下:
<?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_
android:layout_>
<HorizontalScrollView
android:layout_
android:layout_
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_
android:layout_>
<ImageView
android:id="@+id/ivMap"
android:layout_
android:layout_
android:src="@drawable/map"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:contentDescription="@string/map_description" />
<ImageView
android:layout_
android:layout_
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>
还有我的 ViewModel 类:
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)
我无法找出问题所在......
完整的错误日志:
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
【问题讨论】:
【参考方案1】:@BindingAdapter("visible")
fun bindVisible(imageView: ImageView, visible: Boolean)
set visibility according to the boolean
在你的 xml 中写入
app:visible="@viewModel.visible
在你的代码文件中有一个可观察的布尔值
val visible = ObservableBoolean()
【讨论】:
【参考方案2】:当你设置@ 是一种数据绑定,当你使用@= 是两种数据绑定,它需要一个getter。此外,我不会使用默认的可见性属性。我会创建一个新的,比如“show”并传递一个布尔值,并创建一个propper绑定适配器方法,根据该布尔值更改视图可见性,以避免讨厌的东西。
@BindingAdapter("show")
public static void show(View view, boolean bool)
if(bool)
view.setVisibility(View.VISIBLE);
else
view.setVisibility(View.GONE);
【讨论】:
【参考方案3】:您必须在双向绑定中使用@InverseBindingAdapter
getter...参见以下代码:
@JvmStatic
@InverseBindingAdapter(attribute = "value")
fun getValue(knob : ControlKnob) : Int
return knob.value
来源:https://www.onooks.com/android-two-way-data-binding-on-custom-view-cannot-find-getter/
【讨论】:
以上是关于如果绑定适配器提供 getter,请检查适配器是不是正确注释以及参数类型是不是匹配的主要内容,如果未能解决你的问题,请参考以下文章
Android--ListView与数据绑定(Xamarin)