Kotlin Android start new Activity

后端 未结 17 2290
不知归路
不知归路 2020-12-07 23:33

I want to start another activity on Android but I get this error:

Please specify constructor invocation; classifier \'Page2\' does not have a companio

17条回答
  •  不思量自难忘°
    2020-12-08 00:34

    Well, I found these 2 ways to be the simplest of all outcomes:

    Way #1:

    accoun_btn.setOnClickListener {
                startActivity(Intent(this@MainActivity, SecondActivity::class.java))
            }
    

    Way#2: (In a generic way)

        accoun_btn.setOnClickListener {
            startActivity(this)
        }
    
        private inline fun  startActivity(context: Context) {
                startActivity(Intent(context, T::class.java))
            }
    

提交回复
热议问题