AndroidAnnotations - ViewById 不能用于私有(private)元素

标签 android kotlin android-annotations

AndroidAnnotations 版本: 4.3.1

Android编译SDK版本:26

Kotlin 版本:1.1.3-2

我正在尝试使用 Kotlin 和 AndroidAnnotaions 构建应用程序。构建结束于

Error:Execution failed for task ':app:kaptDebugKotlin'. > Internal compiler error. See log for more details

androidannotations.log 中有一大堆错误,例如

00:10:43.908 [RMI TCP Connection(91)-127.0.0.1] ERROR o.a.i.p.ModelValidator:77 - org.androidannotations.annotations.ViewById cannot be used on a private element

这就是@ViewById注解的用法

@ViewById
var description: TextView? = null

Pref 注释的变量也会发生同样的情况。

有其他人面临同样的问题还是只有我?

最佳答案

尝试使用lateinit:

@ViewById
lateinit var description: TextView

出现此错误的原因可能是支持字段的行为。默认情况下它是不可见的,field 标识符只能在属性的访问器中使用。这就是您得到 @ViewById cannot be used on a private element 的原因。

lateinit 起作用的原因是因为它改变了字段的可访问性。根据Kotlin doc :

Late-Initialized properties are also exposed as fields. The visibility of the field will be the same as the visibility of lateinit property setter.

所以,@JvmField 是这个问题的另一种解决方案。

@ViewById
@JvmField var helloTextView: TextView? = null

它还改变了字段的可见性,如文档所述:

If you need to expose a Kotlin property as a field in Java, you need to annotate it with the @JvmField annotation. The field will have the same visibility as the underlying property. You can annotate a property with @JvmField if it has a backing field, is not private, does not have open, override or const modifiers, and is not a delegated property.

您也可以引用这个exampleKotlin docs关于使用注释处理的 Android 框架。

关于AndroidAnnotations - ViewById 不能用于私有(private)元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45701157/

相关文章:

JavaScript 在 Android Webview 中不起作用?

java - 无法在此方法中找到对返回值的引用

Kotlin 优化使用过滤器获取前 n 个元素

android - 如何使用 AndroidAnnotations 在 Android 项目中指定构建顺序

java - Android 没有捕获 Jersey RESTful 异常

android - Gradle构建由于androidannotations插件而失败

android - 更新到 gradle3.1.0-beta1(Android Studio 3.1 Beta1) 后构建应用程序时出现意外错误 "Couldn' t 找不到外部类”

java - 解析数组,其中有对象数组

android - 奥托事件总线 : different Event Classes

generics - 处理Array <T>或IntArray的函数