I have declared the `frameLayout` inside the onCre...
# anko
a
I have declared the
frameLayout
inside the onCreate method. I tried to create
var enableCheckBox: CheckBox
before declaring the frameLayout. But when i tried to access
enableCheckBox
in the rest of the onCreate method, it gave an error saying
Variable "enableCheckBox" must be initialized
c
aakaashjois: you should use something like this: class MyActivity : ... { lateinit var enableCheckBox: CheckBox override fun onCreate() { ..... frameLayout { enableCheckBox = checkBox { text = "Enable" } } .... } }
👍 1
you can use enableCheckBox as normal property, but you need to mark it lateinit as it isn't initialized in class constructor.
a
Thank you! I did not know about the
lateinit
. Since I am just using the variable inside the onCreate method, I thought I can declare it inside onCreate instead of global variable. But it looks like
lateinit
only works with global variable as of now.
y
You can also use
Delegates.notNull()
inside functions.
a
@yan I was not able to understand how
Delegates.notNull()
works 😕