This is my first time building an android application. However when i run the app on my Virtual Device, it stopped working and keeps crashing. The error says something about
It seems like you forgot to initialize date
editText. Why not to try Kotlin Android Extension plugin. By using this plugin you don't need to initialize the views instead you can directly use them using their id. For your case:
Just add import
import kotlinx.android.synthetic.main.activity_add_sales.*
Then you can now remove your:
val date: EditText? = null
val changeDate: CheckBox? = null
val yes: RadioButton? = null
val no: RadioButton? = null
and you can access them by their ids. like
date.setText("15-11-2017")
val date: EditText? = null
val changeDate: CheckBox? = null
val yes: RadioButton? = null
val no: RadioButton? = null
Before using these variables, initialize them with findViewById
or bind them to the xml view.
Your EditText is null. You can initialize your EditText. Example Kotlin code,
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_add_sales)
val date = findViewById<EditText>(R.id.editTextDate)
date.apply {
setText("15-11-2017")
isEnabled = false
}
}
Because your date EditText is null.Before use editText initialise editText like this
var date = findViewById<View>(R.id.date) as? EditText
then set the value of date
date?.setText("15-11-2017")
date?.isEnabled = false