1

I switched from Java to Kotlin and I am completely new. I tried to make an app that calculates your age to minute. The age input is through EditText in Android Studio. Then to set your age into TextView.

When I press on the button that application crashes. Below is the Kotlin Code


        var edtTxtAge = findViewById<EditText>(R.id.edtTxtAge)
        val btnCalc = findViewById<Button>(R.id.btnCalc)
        var txtAge = findViewById<TextView>(R.id.txtAge)

        btnCalc.setOnClickListener(View.OnClickListener {

            val minutesPerYear = 525600
            val age = edtTxtAge.toString().toInt() * minutesPerYear
            txtAge.text = "Your age in minutes is: " + age.toString()

        })

1 Answers1

1

When initialising age you must call "text" before you call to string:

var edtTxtAge = findViewById<EditText>(R.id.edtTxtAge)
    val btnCalc = findViewById<Button>(R.id.btnCalc)
    var txtAge = findViewById<TextView>(R.id.txtAge)

    btnCalc.setOnClickListener(View.OnClickListener {

        val minutesPerYear = 525600
        val age = edtTxtAge.text.toString().toInt() * minutesPerYear
        txtAge.text = "Your age in minutes is: " + age.toString()

    })
Brandon
  • 146
  • 8