Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

i have recently started android development and dunno where i am getting error at

enter image description here

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
}

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading