Android双向绑定转换器@InverseMethod不起作用

古之立大事者,不惟有超世之才,亦必有坚忍不拔之志。这篇文章主要讲述Android双向绑定转换器@InverseMethod不起作用相关的知识,希望能为你提供帮助。
[请帮助。该类中有一个Int字段isNew。不要问为什么要Int,有必要。视图中有一个切换按钮。

< Switch android:id="@+id/switch_is_new_part" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="@dimen/margin_8" android:layout_marginBottom="@dimen/margin_8" android:text="SwitchButtonNew" android:textSize="18sp" android:checked="@={Converter.convertToBoolean(detail.isNew)}" tools:visibility="visible" />

我正在尝试使用双向数据绑定和转换器将int转换为布尔值类转换器
public class Converter {public static boolean convertToBoolean(int checked){ if(checked==1) return true; else return false; }@InverseMethod("convertToBoolean") public static int convertToInt(boolean checked){ if(checked) return 1; else return 0; } }

如果使用单向绑定(@ {Converter.convertToBoolean(detail.isNew)}},则所有内容都会编译。如果我尝试进行双向绑定(@ = {Converter.convertToBoolean(detail.isNew)}),编译会因错误而崩溃:
e: error: cannot generate view binders java.lang.NullPointerException at android.databinding.tool.expr.Expr.lambda$join$0(Expr.java:770) at java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:193) at java.util.Spliterators$ArraySpliterator.forEachRemaining(Spliterators.java:948) at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:481) at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:471) at java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:708) at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234) at java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:499) at android.databinding.tool.expr.Expr.join(Expr.java:771) at android.databinding.tool.expr.Expr.join(Expr.java:766) at android.databinding.tool.expr.MethodCallExpr.computeUniqueKey(MethodCallExpr.java:244) at android.databinding.tool.expr.Expr.getUniqueKey(Expr.java:480) at android.databinding.tool.expr.ExprModel.register(ExprModel.java:120) at android.databinding.tool.expr.CallbackExprModel.register(CallbackExprModel.java:60) at android.databinding.tool.expr.ExprModel.methodCall(ExprModel.java:305) at android.databinding.tool.expr.FieldAccessExpr.generateInverse(FieldAccessExpr.java:428) at android.databinding.tool.expr.MethodCallExpr.generateInverse(MethodCallExpr.java:311) at android.databinding.tool.InverseBinding.< init> (InverseBinding.java:65) at android.databinding.tool.BindingTarget.addInverseBinding(BindingTarget.java:70) at android.databinding.tool.LayoutBinder.< init> (LayoutBinder.java:134) at android.databinding.tool.DataBinder.< init> (DataBinder.java:60) at android.databinding.tool.CompilerChef.ensureDataBinder(CompilerChef.java:117) at android.databinding.tool.CompilerChef.sealModels(CompilerChef.java:369) at android.databinding.annotationprocessor.ProcessExpressions.writeResourceBundle(ProcessExpressions.java:244) at android.databinding.annotationprocessor.ProcessExpressions.onHandleStep(ProcessExpressions.java:126) at android.databinding.annotationprocessor.ProcessDataBinding$ProcessingStep.runStep(ProcessDataBinding.java:247) at android.databinding.annotationprocessor.ProcessDataBinding$ProcessingStep.access$000(ProcessDataBinding.java:232) at android.databinding.annotationprocessor.ProcessDataBinding.doProcess(ProcessDataBinding.java:113) 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:132) at com.sun.tools.javac.processing.JavacProcessingEnvironment.callProcessor(JavacProcessingEnvironment.java:794) at com.sun.tools.javac.processing.JavacProcessingEnvironment.discoverAndRunProcs(JavacProcessingEnvironment.java:705) at com.sun.tools.javac.processing.JavacProcessingEnvironment.access$1800(JavacProcessingEnvironment.java:91) at com.sun.tools.javac.processing.JavacProcessingEnvironment$Round.run(JavacProcessingEnvironment.java:1035) at com.sun.tools.javac.processing.JavacProcessingEnvironment.doProcessing(JavacProcessingEnvironment.java:1176) 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:80) at org.jetbrains.kotlin.kapt3.base.AnnotationProcessingKt.doAnnotationProcessing$default(annotationProcessing.kt:36) at org.jetbrains.kotlin.kapt3.AbstractKapt3Extension.runAnnotationProcessing(Kapt3Extension.kt:223) 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:95) at org.jetbrains.kotlin.cli.jvm.compiler.TopDownAnalyzerFacadeForJVM.analyzeFilesWithJavaIntegration(TopDownAnalyzerFacadeForJVM.kt:105) at org.jetbrains.kotlin.cli.jvm.compiler.TopDownAnalyzerFacadeForJVM.analyzeFilesWithJavaIntegration$default(TopDownAnalyzerFacadeForJVM.kt:80) at org.jetbrains.kotlin.cli.jvm.compiler.KotlinToJVMBytecodeCompiler$analyze$1.invoke(KotlinToJVMBytecodeCompiler.kt:398) at org.jetbrains.kotlin.cli.jvm.compiler.KotlinToJVMBytecodeCompiler$analyze$1.invoke(KotlinToJVMBytecodeCompiler.kt:65) at org.jetbrains.kotlin.cli.common.messages.AnalyzerWithCompilerReport.analyzeAndReport(AnalyzerWithCompilerReport.kt:107) at org.jetbrains.kotlin.cli.jvm.compiler.KotlinToJVMBytecodeCompiler.analyze(KotlinToJVMBytecodeCompiler.kt:389) at org.jetbrains.kotlin.cli.jvm.compiler.KotlinToJVMBytecodeCompiler.compileModules$cli(KotlinToJVMBytecodeCompiler.kt:118) at org.jetbrains.kotlin.cli.jvm.K2JVMCompiler.doExecute(K2JVMCompiler.kt:166) at org.jetbrains.kotlin.cli.jvm.K2JVMCompiler.doExecute(K2JVMCompiler.kt:56) 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$1$1$2.invoke(CompileServiceImpl.kt:442) at org.jetbrains.kotlin.daemon.CompileServiceImpl$compile$1$1$2.invoke(CompileServiceImpl.kt:102) at org.jetbrains.kotlin.daemon.CompileServiceImpl$doCompile$$inlined$ifAlive$lambda$2.invoke(CompileServiceImpl.kt:1005) at org.jetbrains.kotlin.daemon.CompileServiceImpl$doCompile$$inlined$ifAlive$lambda$2.invoke(CompileServiceImpl.kt:102) at org.jetbrains.kotlin.daemon.common.DummyProfiler.withMeasure(PerfUtils.kt:138) at org.jetbrains.kotlin.daemon.CompileServiceImpl.checkedCompile(CompileServiceImpl.kt:1047) at org.jetbrains.kotlin.daemon.CompileServiceImpl.doCompile(CompileServiceImpl.kt:1004) at org.jetbrains.kotlin.daemon.CompileServiceImpl.compile(CompileServiceImpl.kt:441) at sun.reflect.GeneratedMethodAccessor113.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)> Task :app:kaptDebugKotlin FAILEDFAILURE: Build failed with an exception.* What went wrong: Execution failed for task ':app:kaptDebugKotlin'. > Compilation error. See log for more details

我在哪里弄错了?
答案注解@InverseMethod应该添加到双向绑定表达式中调用的函数中。所以
public class Converter {public static boolean convertToBoolean(int checked)@InverseMethod("convertToBoolean") public static int convertToInt(boolean checked) }

【Android双向绑定转换器@InverseMethod不起作用】应更改为
public class Converter {@InverseMethod("convertToInt") public static boolean convertToBoolean(int checked)public static int convertToInt(boolean checked) }


    推荐阅读