Home > Enterprise >  I am learning Android studio and doing a rectangle calculator, my kotlin has 19 errors and cannot fi
I am learning Android studio and doing a rectangle calculator, my kotlin has 19 errors and cannot fi

Time:07-02

I am learning Android studio and doing a rectangle calculator. My Kotlin has 19 errors, and I cannot figure it out. I keep getting unresolved errors for btn, functions that cannot be called, and expecting an element. I am trying to do a calculator that takes height and width and then calculates area and perimeter. Just need guidance on what I am doing wrong and not looking for someone to give me new code.

MainActivity.kt

package com.example.calculator

import android.annotation.SuppressLint
import android.icu.text.DecimalFormat
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.*

class MainActivity : AppCompatActivity() {

    @SuppressLint("SetTextI18n")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        btn_calculate.setOnClickListener {
            calculate()
            btn_calculate.onEditorAction(EditorInfo.IME_ACTION_DONE)
        }
        btn_reset.setOnClickListener {
            reset()
        }
    }
    private fun calculate() {
        val formatter = DecimalFormat("#.##")
        val editNum1 = (EditText) editNum1.text.toString()
        val editNum2 = (EditText) editNum2.text.toString()

        val Area = DecimalFormat(editNum1.toDouble() * editNum2.toDouble())
        val Perimeter = DecimalFormat(2* ( (editNum1.toDouble())   (editNum2.toDouble()))
    }}

CodePudding user response:

If you want to access the views from your XML file directly in your activity you should use Kotlin Android Extension. But this plugin is deprecated. You should migrate to View Binding.

https://developer.android.com/topic/libraries/view-binding/migration

  • Related