Search code examples
android-studiokotlinvarimagebuttoncardview

Keep getting error "Conflicting declarations: val open: ImageButton, val open: ImageButton"


I'm still in the proccess of creating my app. I'm trying to make it to where when i click the imabe button it will go to a new activity. each image button has it's on activity.

code*

`

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


    val open: ImageButton = findViewById<ImageButton>(R.id.card1)
    open.setOnClickListener(View.OnClickListener {
        val intent = Intent(this@MainActivity, TMs::class.java)
        startActivity(intent)
    })

    val open: ImageButton = findViewById<ImageButton>(R.id.card2)
    open.setOnClickListener(View.OnClickListener {
        val intent = Intent(this@MainActivity, Schematics::class.java)
        startActivity(intent)
    })

    val open: ImageButton = findViewById<ImageButton>(R.id.card3)
    open.setOnClickListener(View.OnClickListener {
        val intent = Intent(this@MainActivity, PartsCheatSheets::class.java)
        startActivity(intent)
    })

    val open: ImageButton = findViewById<ImageButton>(R.id.card4)
    open.setOnClickListener(View.OnClickListener {
        val intent = Intent(this@MainActivity, GeneratorLoadWiring::class.java)
        startActivity(intent)
    })

    val open: ImageButton =findViewById<ImageButton>(R.id.card5)
    open.setOnClickListener(View.OnClickListener {
        val intent = Intent(this@MainActivity, Hdt::class.java)
        startActivity(intent)
    })

    val open: ImageButton =findViewById<ImageButton>(R.id.card6)
    open.setOnClickListener(View.OnClickListener {
        val intent = Intent(this@MainActivity, GCP::class.java)
        startActivity(intent)
    })

    val open: ImageButton =findViewById<ImageButton>(R.id.card7)
    open.setOnClickListener(View.OnClickListener {
        val intent = Intent(this@MainActivity, SP::class.java)
        startActivity(intent)
    })

    val open: ImageButton =findViewById<ImageButton>(R.id.card8)
    open.setOnClickListener(View.OnClickListener {
        val intent = Intent(this@MainActivity, Cummins::class.java)
        startActivity(intent)
    })

}

}`

when its set up like this it works but only when i click the first one then go to the second one and so on down the list.

code*

`

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


    val open: ImageButton = findViewById<ImageButton>(R.id.card1)
    open.setOnClickListener(View.OnClickListener {
        val intent = Intent(this@MainActivity, TMs::class.java)
        startActivity(intent)

        val open: ImageButton = findViewById<ImageButton>(R.id.card2)
        open.setOnClickListener(View.OnClickListener {
            val intent = Intent(this@MainActivity, Schematics::class.java)
            startActivity(intent)

            val open: ImageButton = findViewById<ImageButton>(R.id.card3)
            open.setOnClickListener(View.OnClickListener {
                val intent = Intent(this@MainActivity, PartsCheatSheets::class.java)
                startActivity(intent)

                val open: ImageButton = findViewById<ImageButton>(R.id.card4)
                open.setOnClickListener(View.OnClickListener {
                    val intent = Intent(this@MainActivity, GeneratorLoadWiring::class.java)
                    startActivity(intent)

                    val open: ImageButton =findViewById<ImageButton>(R.id.card5)
                    open.setOnClickListener(View.OnClickListener {
                        val intent = Intent(this@MainActivity,Hdt::class.java)
                        startActivity(intent)

                        val open: ImageButton =findViewById<ImageButton>(R.id.card6)
                        open.setOnClickListener(View.OnClickListener {
                            val intent = Intent(this@MainActivity,GCP::class.java)
                            startActivity(intent)

                            val open: ImageButton =findViewById<ImageButton>(R.id.card7)
                            open.setOnClickListener(View.OnClickListener {
                                val intent = Intent(this@MainActivity,SP::class.java)
                                startActivity(intent)

                                val open: ImageButton =findViewById<ImageButton>(R.id.card8)
                                open.setOnClickListener(View.OnClickListener {
                                    val intent = Intent(this@MainActivity,Cummins::class.java)
                                    startActivity(intent)
                                })
                            })
                        })
                    })
                })
            })
        })
    })

}

}`


Solution

  • Change:

    val open: ImageButton = findViewById<ImageButton>(R.id.card1)
    open.setOnClickListener(View.OnClickListener {
        val intent = Intent(this@MainActivity, TMs::class.java)
        startActivity(intent)
    })
    

    To:

    val card1: ImageButton = findViewById<ImageButton>(R.id.card1)
    card1.setOnClickListener(View.OnClickListener {
        val intent = Intent(this@MainActivity, TMs::class.java)
        startActivity(intent)
    })
    

    And do like this for the other buttons as well