【问题标题】:How to use AndroidAnnotation @SharedPref with Kotlin如何在 Kotlin 中使用 AndroidAnnotation @SharedPref
【发布时间】:2017-07-07 13:39:18
【问题描述】:

我正在尝试在 kotlin 中使用 AndroidAnnotations @SharefPref,但出现以下错误

org.androidannotations.annotations.sharedpreferences.Pref can only be used on an element that extends org.androidannotations.api.sharedpreferences.SharedPreferencesHelper

我做错了什么?

//Interface
@SharedPref(SharedPref.Scope.APPLICATION_DEFAULT)
open interface MyPreferences {
    @DefaultInt(-1)
    fun someIntValue():Int
}

//Fragment
@Pref
lateinit open var sharedPref:CongressPreferences_

//usage within fragment
val get: Int = sharedPref.selectedEventId().get()

【问题讨论】:

  • 错误信息不是不言自明吗? MyPreferences 不扩展 SharedPreferencesHelper
  • @azizbekian 不,不是。使用KotlinAnnotations 有时会抛出与实际问题无关的错误。如果生成了带注释的代码,就会发生这种情况。 (见下面的解决方案)

标签: android kotlin android-annotations kotlin-android-extensions


【解决方案1】:

这是由于 Kotlin 注释处理器中的 bug 造成的。
要解决此问题,请将you must add correctErrorTypes = true 添加到您的kapt 块中。

kapt {
  correctErrorTypes = true
}

还要确保您使用的是最新的 Kotlin 版本(截至目前:1.1.3)。

【讨论】:

【解决方案2】:

我只想扩展@WonderCsabo 的答案。 他的回答几乎救了我,但不是完全的。

将此添加到我的应用标签构建 gradle 后。

kapt { correctErrorTypes = true }

我无法运行我的应用程序。

然后我关闭了我的 android studio,然后以管理员身份再次运行 Android studio。 瞧!它就像魅力一样。

谢谢@WonderCsabo

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-12-25
    • 2019-10-12
    • 2015-11-03
    • 1970-01-01
    • 1970-01-01
    • 2019-05-19
    • 2020-09-24
    • 2021-10-01
    相关资源
    最近更新 更多