CloakedArrow
CloakedArrow

Reputation: 433

How to code more than 1 button in mainactivity without errors?

I am working on a little project using Kotlin in Android Studio and I keep getting an error which I cannot make sense of. When I add 'sdkButton_2' part in the code, OpenDialogue box becomes an unresolved reference. I know this is not correct because it works fine when I just have that one button. I have tried adding a separate function for each button but that obviously doesn't work, and I'm not too sure what else to do from here. Any ideas would be appreciated. I've copied and pasted the code below. (OpenDialogueBox is a funtion I've added further down in the code. I don't think its relevant to the current issue but let me know if you need to see it).

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

    var sdkButton = findViewById<Button>(R.id.sdk_button)
    sdkButton.setOnClickListener{
        openDialogueBox()

val sdkButton_2 = findViewById<Button>(R.id.sdk_button_2)
    sdkButton_2.setOnClickListener{
        val intent = Intent(this, secondaryactivity::class.java)
        startActivity(intent);
    }

Upvotes: 0

Views: 72

Answers (3)

Avinash Nath
Avinash Nath

Reputation: 132

Try something like this, one bracket is missing or you have some different name in the XML file. check once

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

var sdkButton = findViewById<Button>(R.id.sdk_button)
sdkButton.setOnClickListener{
    openDialogueBox()
}

val sdkButton_2 = findViewById<Button>(R.id.sdk_button_2)
sdkButton_2.setOnClickListener{
    val intent = Intent(this, secondaryactivity::class.java)
    startActivity(intent);
}

Upvotes: 0

Sven
Sven

Reputation: 46

I think it has something to do with the fact that you are not closing the braces of the buttonName.setOnClickListener at both buttons.

Upvotes: 1

Einar Sundgren
Einar Sundgren

Reputation: 4433

I assume you did not define the R.id.sdk_button_2 in the xml file. Share your xml too so we can say for certain.

Upvotes: 0

Related Questions