【发布时间】:2018-06-20 14:07:56
【问题描述】:
我正在尝试在布局中使用伴随对象属性,但编译器无法识别它。
Kotlin 类
class MyClass {
companion object {
val SomeProperty = "hey"
}
}
XML 布局
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:fancy="http://schemas.android.com/tools">
<data>
<import type="package.MyClass"/>
</data>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@{MyClass.Companion.SomeProperty}"/>
</layout>
我得到了这个错误:
e: java.lang.IllegalStateException: failed to analyze: android.databinding.tool.util.LoggedErrorException: Found data binding errors.
****/ data binding error ****msg:Could not find accessor package.MyClass.Companion.SomeProperty file:/path/to/my/layout.xml loc:21:67 - 21:103 ****\ data binding error ****
at org.jetbrains.kotlin.analyzer.AnalysisResult.throwIfError(AnalysisResult.kt:57)
at org.jetbrains.kotlin.cli.jvm.compiler.KotlinToJVMBytecodeCompiler.compileModules(KotlinToJVMBytecodeCompiler.kt:138)
at org.jetbrains.kotlin.cli.jvm.K2JVMCompiler.doExecute(K2JVMCompiler.kt:154)
...
Caused by: android.databinding.tool.util.LoggedErrorException: Found data binding errors.
****/ data binding error ****msg:Could not find accessor package.MyClass.Companion.SomeProperty file:/path/to/my/layout.xml loc:21:67 - 21:103 ****\ data binding error ****
at android.databinding.tool.processing.Scope.assertNoError(Scope.java:112)
at android.databinding.annotationprocessor.ProcessDataBinding.doProcess(ProcessDataBinding.java:101)
at android.databinding.annotationprocessor.ProcessDataBinding.process(ProcessDataBinding.java:65)
...
我尝试使用companion 代替Companion,但没有成功。
是否可以在带有数据绑定的 xml 布局中使用伴随对象?我该如何进行?提前感谢您的帮助:)
【问题讨论】:
-
上下文是什么?你为什么要这个
-
好吧,我想要一个 if 将一个值与一个常数进行比较,例如:
android:visibility="@{delivery.status == Status.Companion.finished ? View.VISIBLE : View.GONE}"我不知道,我只是编造的,但是像这样
标签: android kotlin android-databinding