kotlin 我能知道我的程序出了什么问题吗?它成功运行,但在Main3Activity学习中没有执行瓦尔按钮3,请帮助

w51jfk4q  于 2023-02-05  发布在  Kotlin
关注(0)|答案(1)|浏览(81)
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main2)

    val button2 = findViewById<Button>(R.id.button2)
    val button3= findViewById<Button>(R.id.button3)

    button2.setOnClickListener {
        val intent = Intent(this, MainActivity::class.java)
        startActivity(intent)

    button3.setOnClickListener {
         val intent = Intent(this, Main3Activity::class.java)
         startActivity(intent)

     }
    }

button3.setOnClickListener {瓦尔意图=意图(此,主3活动::class.java)开始活动(意图)
未执行,请协助

jgwigjjp

jgwigjjp1#

您似乎错过了关闭button2.setOnClickListener上的花括号},并将button3.setOnClickListener放入button 2单击侦听器中。只需关闭button 2单击侦听器中的startActivity(intent)后的括号,即可正常工作。

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

val button2 = findViewById<Button>(R.id.button2)
val button3= findViewById<Button>(R.id.button3)

button2.setOnClickListener {
    val intent = Intent(this, MainActivity::class.java)
    startActivity(intent)
}

button3.setOnClickListener {
     val intent = Intent(this, Main3Activity::class.java)
     startActivity(intent)
}

相关问题