0

Unresolved reference: toDoubleOrNull , ceil and few other errors are shown in Android studo. It is running without any issues but it is highlighted in red and showing as reference issue. can someone help me to get rid of these errors?

enter image description here

Full Code:

package com.yosuva.tiptime


import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import com.yosuva.tiptime.databinding.ActivityMainBinding
import java.text.NumberFormat

class MainActivity : AppCompatActivity() {

    private lateinit var binding: ActivityMainBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)

        binding.calculateButton.setOnClickListener { calculateTip() }
    }

    private fun calculateTip() {
        val stringInTextField = binding.costOfService.text.toString()
        val cost = stringInTextField.toDoubleOrNull()
        if (cost == null) {
            binding.tipResult.text = ""
            return
        }

        val tipPercentage = when (binding.tipOptions.checkedRadioButtonId) {
            R.id.option_twenty_percent -> 0.20
            R.id.option_eighteen_percent -> 0.18
            else -> 0.15
        }

        var tip = tipPercentage * cost
        if (binding.roundUpSwitch.isChecked) {
            tip = kotlin.math.ceil(tip)
        }

        val formattedTip = NumberFormat.getCurrencyInstance().format(tip)
        binding.tipResult.text = getString(R.string.tip_amount, formattedTip)
    }
}
Yosuva Arulanthu
  • 1,444
  • 4
  • 18
  • 32

1 Answers1

0

Try to invalidate restart Android Studio, File -> Invalidate Cache / Restart ..

or Make sure you're using latest version of Kotlin.

Ankush
  • 121
  • 1
  • 4
  • I have tried invalidate cache and restart but it is behaving in same way. and I am using ext.kotlin_version = "1.6.0" – Yosuva Arulanthu Nov 27 '21 at 10:53
  • @YosuvaArulanthu You can try upgrading com.android.tools.build:gradle to stable one. or check this link https://stackoverflow.com/questions/51621301/android-studio-3-1-3-unresolved-reference-r-kotlin – Ankush Nov 28 '21 at 16:02