Solution 1 :
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
Problem :
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()))
}}
Comments
Comment posted by Elango
Can you show the error to us
Comment posted by Abhishek Dutt
Post the stack trace or the error log you are getting.
Comment posted by Abhishek Dutt
Seems like you forgot to define
Comment posted by deprecated
Kotlin extensions is
Comment posted by Jensy
sorry for sounding dumb but I just started Android studio this week and it is an online course and the text book used is from 2015. So Kotlin extension in the import library is no longer used and I have to do view-binding?
Comment posted by cactustictacs
@Jensy yeah the synthetic binding stuff was a bit “magical” and it’s been deprecated for a long time – the good news is View Binding works almost exactly the same way, you just have to explicitly create a binding object and reference all your views on that. So
Comment posted by Jensy
Thank you will try the steps