Kotlin批注参数必须是编译时常量 [英] Kotlin annotation parameter must be a compile-time constant

查看:1159
本文介绍了Kotlin批注参数必须是编译时常量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

@BindView(R.id.et_login_username)
internal var loginUsername: EditText? = null

Kotlin注释参数必须为编译时常量

Kotlin annotation parameter must be a compile-time constant

这是显示的错误.

推荐答案

要在Kotlin中使用ButterKnife,请确保已在 app gradle 中添加了以下依赖项.

To use ButterKnife in Kotlin, make sure you have added the following dependencies in app gradle.

apply plugin: 'kotlin-android'
apply plugin: 'kotlin-kapt'
apply plugin: 'kotlin-android-extensions'

dependencies {

    implementation 'com.jakewharton:butterknife:latest-version'

    // use kapt for kotlin
    kapt 'com.jakewharton:butterknife-compiler:latest-version'
}

在您的活动中,使用lateinit声明视图,以避免编译时常量错误:

In your activity, declare views using lateinit to avoid compile-time constant error:

@BindView(R.id.et_login_username)
lateinit var loginUsername: EditText


此外,Kotlin开发人员还引入了绑定android视图的替代方法,从而消除了findViewById调用.它称为 Kotlin Android扩展


Moreover, Kotlin developers also introduced an alternative for binding android views which eliminates findViewById calls. Its known as Kotlin Android Extensions,

要使用此功能:

应用的 build.gradle中,添加此插件

apply plugin: 'kotlin-android-extensions'

在活动中,

import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

       // All views can be used directly with their id declared in the xml
       et_login_username.setText("Hello")
}

这篇关于Kotlin批注参数必须是编译时常量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆