Kotlin - app crashing with complex calculations, would appreciate help

class MainActivity : AppCompatActivity() {

    private lateinit var binding: ActivityMainBinding
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)

        val age = binding.etAge.text
        binding.tvSex.text
        val weight = binding.etWeight.text
        val height = binding.etHeight.text
        val creatinine = binding.etCreatinine.text

        val btnCalculate = findViewById<Button>(R.id.btnCalculate)
        val btnReset = findViewById<Button>(R.id.btnReset)

        val female = binding.rbFemale

        val pounds = binding.rbPounds

        val inches = binding.rbInches
        val mg = binding.rbMg

        var tvBMIv: Any?
        var tvBSAv: Any?
        var tvCrClv: Any?
        var tvEGFRv: Any?
        var tvGFRv: Any? = Any()

        val tvBMI = binding.tvBMIv
        val tvBSA = binding.tvBSAv
        val tvClearance = binding.tvCrclv
        val tveGFR = binding.tvEGFRv
        val tvGFR = binding.tvGFRv



        btnCalculate.setOnClickListener {

            //hideKeyboard()

            // Conversions to SI units

            val weightKg: Any?
            val heightCm: Any?
            val creatumol: Any?


            if ((age.isNullOrBlank()) || (weight.isNullOrBlank()) || (height.isNullOrBlank()) || (creatinine.isNullOrBlank())) {
                Toast.makeText(
                    this,
                    "Please assure all values are entered!",
                    Toast.LENGTH_LONG
                ).show()
            }

            weightKg = if (pounds.isChecked) {
                weight.toString().toDouble() / 2.2
            } else {
                weight
            }

            heightCm = if (inches.isChecked) {
                height.toString().toDouble() * 2.54
            } else {
                height
            }

            creatumol = if (mg.isChecked) {
                creatinine.toString().toDouble() * 88.4
            } else {
                creatinine
            }


            tvBMIv = String.format(
                "%.2f",
                (weightKg.toString().toDouble() / (heightCm.toString().toDouble() / 100).pow(2)
                    .toString().toDouble()).toString()
            )
            tvBMI.text = tvBMIv as String
            
            tvBSAv = String.format("%.2f",(0.20247 * (heightCm.toString().toDouble()/100).pow(0.725) * (weightKg.toString().toDouble().pow(0.425))).toString())
                tvBSA.text = tvBSAv as String

            tvCrClv = if (female.isChecked) {
                String.format("%.2f", ((140 - age.toString().toDouble()) * weightKg.toString().toDouble() / creatumol.toString().toDouble()) * 1.04)
            } else {
                String.format("%.2f", ((140 - age.toString().toDouble()) * weightKg.toString()
                    .toDouble() / creatumol.toString().toDouble()) * 1.23)
            }
                tvClearance.text = tvCrClv as String

            tvEGFRv = if (female.isChecked) {
                String.format("%.2f",(144 * (creatumol.toString().toDouble() / 61.88 * (0.993).pow(age.toString().toDouble()))).toString())
            } else {
                String.format("%.2f", (144 * (creatumol.toString().toDouble() / 79.56.toString()
                    .toDouble()) * (0.993).pow(age.toString().toDouble())).toString())
            }
                tveGFR.text = tvEGFRv as String

            tvGFRv = String.format("%.2f", (tvEGFRv.toString().toDouble()/1.73) * tvBSAv.toString().toDouble())
        }
                tvGFR.text = tvGFRv as String

            btnReset.setOnClickListener {
                age.clear()
                weight.clear()
                height.clear()
                tvBMI.text = ""
                tvBSA.text = ""
                tvClearance.text = ""
                tveGFR.text = ""
                tvGFR.text = ""

                

        }
    }


}


Could this be because I am performing multiple and complex calculations with one button click. Code is written using Kotlin. Android studio doesn’t give any errors in code in either activity_main.xml or manifest.kt files. If I comment off the code after “Calculate button”, the app is working up to that level, so not clear where the issue is. Thanks

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.