Home > Blockchain >  kotlin doesn't recognize costOfService
kotlin doesn't recognize costOfService

Time:08-14

kotlin doesn't recognize costOfService and says that it needs to be renamed. How can I solve this problem?

package com.example.tiptime

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import com.example.tiptime.databinding.ActivityMainBinding

class MainActivity : AppCompatActivity() {

    lateinit var binding: ActivityMainBinding

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

        binding.colculateButton.setOnClickListener { calculateTip() }
    }

    fun calculateTip() {
        val costText: String = binding.costOfService.text.toString()
        val cost: Double = costText.toDouble()
    }
} ``` 

CodePudding user response:

Check whether the ID of the view you are mentioning ("costOfService") is named the same in your layout, eg.:

xml:

<LinearLayout ... >

    <TextView android:id="@ id/NAME" />

    <ImageView android:cropToPadding="true" />

    <Button android:id="@ id/button"
        android:background="@drawable/rounded_button" />

</LinearLayout>

In an activity/fragment (after the binding variable was initialised)

binding.NAME.text = viewModel.name
binding.button.setOnClickListener { viewModel.userClicked() }
  • Related