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

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

private TextView mSomeTextView; 

然后我们在一些方法中调用findViewById。 但是在科特林,我不能只写这样的东西,我需要:

 private val textView: TextView = findViewById(R.id.text) 

我按照以前的方式写在onCreate下。 问题:对吗? 如果不是 – 我应该在哪里以及如何做?

你应该使用lateinit

 private lateinit var textView: TextView override fun onCreate(savedInstanceState: Bundle?) { ... textView = findViewById(R.id.text) } 
Interesting Posts