this is in Kotlin, it doesn't seem to work no matter what I try it's just a button with a click listener. Its function is to take me to another activity, I tried a toast but didn't show either. I tried not using the function and also didn't work. and can we use this method with a text view? I'm new in Kotlin so easy on me...
here's the code
val startButton = binding.loginButton
startButton.setOnClickListener {
fun crtUser() {
Toast.makeText(this, "It's Working!", Toast.LENGTH_LONG).show()
val intent = Intent(this@LoginActivity, SignupActivity::class.java)
startActivity(intent)
}
crtUser()
}
}
I also used finish() after the Intent, and it crashed
CodePudding user response:
Please do not use this syntax, it is not invalid, but improving it will be much better for you and for other developers to understand faster and easily what is going on :). Trigger the function from the on click event as follows:
fun onCreateOrOtherMethod() {
binding.button.setOnClickListener {
createUser()
}
}
And then you can have createUser() as an inner method of your current class:
private fun createUser() {
Toast.makeText(this, "It's Working!", Toast.LENGTH_LONG).show()
val intent = Intent(this@LoginActivity, SignupActivity::class.java)
startActivity(intent)
}
Little tip: don't abbreviate the methods names, you can use long name if needed as long as it improves the semantics of your code.
If it crashes, please, attach the Exception stack trace :D
CodePudding user response:
It's working now just after I changed this
setContentView(R.layout.login_activity)
To this
setContentView(binding.root)
Any Idea Why??