如何使用Google代码样式在Android(Kotlin)中声明变量?

Vol*_*yrH 3 coding-style kotlin

我开始在Kotlin中构建应用程序,我想知道如何正确初始化变量。例如在Java中,它就像:

 private TextView mSomeTextView;
Run Code Online (Sandbox Code Playgroud)

然后在某些方法中调用findViewById。但是在Kotlin中,我不能只写这样的东西,我需要:

private val textView: TextView = findViewById(R.id.text)
Run Code Online (Sandbox Code Playgroud)

我像往常一样在onCreate下编写它。问题:合适的地方吗?如果没有,我应该在哪里以及如何做?

Ruc*_*oom 6

您应该使用lateinit

private lateinit var textView: TextView

override fun onCreate(savedInstanceState: Bundle?) {
    ...
    textView = findViewById(R.id.text)
}
Run Code Online (Sandbox Code Playgroud)