Home > Net >  Smart cast to 'Type!' is impossible, because 'variable' is a mutable property th
Smart cast to 'Type!' is impossible, because 'variable' is a mutable property th

Time:08-15

I would really appreciate if someone will help me.

I'm trying to make an app using kotlin for android and am running into problems because I get errors like "Smart cast to 'Button!' is impossible, because 'activityButton1' is a mutable property that could have been changed by this time" I would really appreciate if someone will help me to fix this problem. Here is my code-

import android.content.Intent
import android.os.Bundle
import android.view.View
import android.widget.Button
import android.widget.Spinner
import androidx.appcompat.app.AppCompatActivity


class MainActivity : AppCompatActivity() {
    private var activityButton1: Button? = null
    private var spinnerList: Spinner? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        activityButton1 = findViewById(R.id.clickMe)
        spinnerList = findViewById(R.id.spinner)
        val intentTo3 = Intent(this@MainActivity, ThirdActivity::class.java)
        val intent = Intent(this@MainActivity, SecondActivity::class.java)
        activityButton1.setOnClickListener(View.OnClickListener {
            val optionSelected = spinnerList.getSelectedItem().toString()
            if (optionSelected.equals("Custom BroadCast Receiver", ignoreCase = true)) {
                intent.putExtra("name", "custom")
                startActivity(intent)
            } else if (optionSelected.equals(
                    "System Battery notification receiver",
                    ignoreCase = true
                )
            ) {
                intent.putExtra("name", "battery")
                startActivity(intent)
            } else if (optionSelected.equals("Wifi RTT state change receiver", ignoreCase = true)) {
                intentTo3.putExtra("name", "rtt")
                startActivity(intentTo3)
            }
        })
    }
}

CodePudding user response:

Change your views like this:

private lateinit var activityButton1: Button
private lateinit var spinnerList: Spinner

This post may help you for further information

CodePudding user response:

Kotlin can access view element without having findViewById() method.

You can read in develop: https://developer.android.com/topic/libraries/view-binding

CodePudding user response:

You have two mutable properties :

private var activityButton1: Button? = null

and

private var spinnerList: Spinner? = null

If you want to use those values freely you have two solutions :

Using another val to store value

When you want to use a mutable property at some point of your code, you can simply create an immutable variable (val) that will old the value of your mutable property.

Using lateint

The best way is to use a lateinit property.

 private lateinit var activityButton1: Button
 private lateinit var spinnerList: Spinner

with lateinit property you ensure that at one point you will give a value to the properties. If you are trying to access the value before initializing, it will throw an error,

  • Related