Home > database >  Keep getting error "Conflicting declarations: val open: ImageButton, val open: ImageButton"
Keep getting error "Conflicting declarations: val open: ImageButton, val open: ImageButton"

Time:03-31

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)
                                })
                            })
                        })
                    })
                })
            })
        })
    })

}

}`

CodePudding user response:

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

CodePudding user response:

Do it like

 val open1: ImageButton = findViewById<ImageButton>(R.id.card1)
        val open2: ImageButton = findViewById<ImageButton>(R.id.card2)
        val open3: ImageButton = findViewById<ImageButton>(R.id.card3)
        val open4: ImageButton = findViewById<ImageButton>(R.id.card4)
        val open5: ImageButton =findViewById<ImageButton>(R.id.card5)
        val open6: ImageButton =findViewById<ImageButton>(R.id.card6)
        val open7: ImageButton =findViewById<ImageButton>(R.id.card7)
        val open8: ImageButton =findViewById<ImageButton>(R.id.card8)

      
        open1.setOnClickListener(View.OnClickListener {
            val intent = Intent(this@MainActivity, TMs::class.java)
            startActivity(intent)
        })

        open1.setOnClickListener(View.OnClickListener {
            val intent = Intent(this@MainActivity, Schematics::class.java)
            startActivity(intent)
        })

        open2.setOnClickListener(View.OnClickListener {
            val intent = Intent(this@MainActivity, PartsCheatSheets::class.java)
            startActivity(intent)
        })

        open3.setOnClickListener(View.OnClickListener {
            val intent = Intent(this@MainActivity, GeneratorLoadWiring::class.java)
            startActivity(intent)
        })

        open4.setOnClickListener(View.OnClickListener {
            val intent = Intent(this@MainActivity, Hdt::class.java)
            startActivity(intent)
        })

        open5.setOnClickListener(View.OnClickListener {
            val intent = Intent(this@MainActivity, GCP::class.java)
            startActivity(intent)
        })

        open6.setOnClickListener(View.OnClickListener {
            val intent = Intent(this@MainActivity, SP::class.java)
            startActivity(intent)
        })

        open7.setOnClickListener(View.OnClickListener {
            val intent = Intent(this@MainActivity, Cummins::class.java)
            startActivity(intent)
        })
  • Related