Home > OS >  how to develop a button menu in the main activities with kotlin
how to develop a button menu in the main activities with kotlin

Time:06-09

I am trying to build an app where users have have the option of selecting from a list of calculation buttons in the main activity. The selection of each button would direct the user to the respective activity. Please see my code attempt using kotlin. My app crashes whenever I select an option. How could this issue be solved? enter image description here

    package com.example.myapplication
    
    import android.content.Intent
    import androidx.appcompat.app.AppCompatActivity
    import android.os.Bundle
    import android.widget.Button
    
    class MainActivity : AppCompatActivity() {
    private var btnprobate: Button? = null
    private var btnconveyance: Button? = null
    
    override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)
    
    val btnprobate : Button = findViewById(R.id.btn_probate)
    val btnconveyance: Button = findViewById(R.id.btn_conveyance)
    
    btnprobate.setOnClickListener{
    val probateintent = Intent(this, ProbateActvity::class.java)
    startActivity(probateintent)
    }
    btnconveyance.setOnClickListener{
    val conveyanceintent = Intent(this, ConveyanceActivity::class.java)
    startActivity(conveyanceintent)
    
    }
    }
    }

CodePudding user response:

private var btnprobate: Button? = null

private var btnconveyance: Button? = null

you need to delete those 2 lines under class MainActivity : AppCompatActivity() since you already declare it inside onCreate method.

CodePudding user response:

You are overriding the btnprobate and btnconveyance global declarations.
Change your code like this:

class MainActivity : AppCompatActivity() {

    private var btnprobate: Button? = null
    private var btnconveyance: Button? = null
    
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        
        btnprobate = findViewById(R.id.btn_probate)
        btnconveyance = findViewById(R.id.btn_conveyance)
        
        btnprobate?.setOnClickListener{
            val probateintent = Intent(this, ProbateActvity::class.java)
            startActivity(probateintent)
        }
        btnconveyance?.setOnClickListener{
            val conveyanceintent = Intent(this, ConveyanceActivity::class.java)
            startActivity(conveyanceintent)
        }
    }
}

Anyway, You can improve your implementation by using view binding:

class MainActivity : AppCompatActivity() {

    private lateinit var binding: ActivityMainBinding
    
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)
        
        binding.btnProbate.setOnClickListener{
            val probateintent = Intent(this, ProbateActvity::class.java)
            startActivity(probateintent)
        }
        binding.btnConveyance.setOnClickListener{
            val conveyanceintent = Intent(this, ConveyanceActivity::class.java)
            startActivity(conveyanceintent)
        }
    }
}
  • Related