Android studio >> Buttons do not work properly

Viewed 41

Second and third buttons work only if I click the first button first. If I click on the second or third button before trying first button then the buttons do not work. Can you please help me to find out my mistakes? IDE: Android Studio, Language: Kotlin.

Codes are here:

package com.example.habiganjkotlin
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {

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




            info_button.setOnClickListener {
                val intent = Intent(this, habiganjinfo::class.java)
                // start your next activity
                startActivity(intent)

                news_button.setOnClickListener {
                    val intent = Intent(this, habiganjnews::class.java)
                    // start your next activity
                    startActivity(intent)


                corona_button.setOnClickListener {
                     val intent = Intent(this, corona::class.java)
                     // start your next activity
                     startActivity(intent)


    }   }   }   }   }
1 Answers

The problem of your code is that you have put button 2 and button 3 in button1.setOnClickListener. If these three buttons are independent and you can choose any of them, put them in three seperate setOnClickListener. I mean:

Button1.setOnClickListener{...}
Button2.setOnClickListener{...}

But you have written:

Button1.setOnClickListener{...
Button2.setOnClickListener{...}}

And this is wrong.

Related