I am getting some kind of empty string Amount function but dont know as im passing not null and non empty string
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val editText: EditText = findViewById(R.id.cost_of_service)
val radioGroup: RadioGroup = findViewById(R.id.tip_options)
val cal: Button = findViewById(R.id.calculate_button)
val message: String = editText.text.toString()
val tv: TextView = findViewById(R.id.tip_result)
getSignal(radioGroup, cal, message, tv)
}
private fun getSignal(radioGroup: RadioGroup, cal: Button, message: String, tv: TextView){
cal.setOnClickListener{
val selectedRB: RadioButton = findViewById(radioGroup!!.checkedRadioButtonId) //selectedRB -> selected radio button id
val per: String = selectedRB.text.toString()
val amount: Double = Amount(per, message)
tv.text = amount.toString()
}
}
private fun Amount(per: String, message: String): Double {
return message.toDouble() (message.toInt() * per.toInt()) / 100
}
CodePudding user response:
val message: String = editText.text.toString()
That gets the value of the editText at the time its called. It does not update it as it changes. Since you're calling this in onCreate, its almost certainly empty. And since it's empty, its not a numbver so trying to convert it to one fails.
The fix for this is to put this inside your onClickListener. Do the same for every field you want to get the text out of.