Reputation: 2019
I started to build the app in Kotlin and I want to know how to correctly initialize variables. For example in Java it was like:
private TextView mSomeTextView;
And then we call findViewById in some methods. But in Kotlin I can't just write something like that, I need to:
private val textView: TextView = findViewById(R.id.text)
I write it under onCreate as I used to. Question: is it right place for it? If no -- where and how should I do it?
Upvotes: 4
Views: 4719
Reputation: 4786
You should use lateinit
:
private lateinit var textView: TextView
override fun onCreate(savedInstanceState: Bundle?) {
...
textView = findViewById(R.id.text)
}
Upvotes: 15