0

enter image description hereI have a problem calling a second activity using a password recovery button. I get the app stopped.

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View

class MainActivity : AppCompatActivity() {

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

    }

    fun goToRecover(view: View) {
        val intent = Intent(this,RecoverPasswordActivity::class.java)
        this.startActivity(intent)
    }
}

enter image description here

enter image description here

enter image description here

Barbora
  • 921
  • 1
  • 6
  • 11
  • Have you declared RecoverPasswordActivity correctly in the AndroidManifest.xml? Please take a look at similar stackoverflow posts: https://stackoverflow.com/questions/10908534/android-content-activitynotfoundexception-unable-to-find-explicit-activity-clas https://stackoverflow.com/questions/37293314/unable-to-find-explicit-activity-class-have-you-declared-this-activity-in-your – Feedbacker May 26 '20 at 08:38

1 Answers1

0

Did you try adding the onClick in the XML-layout like this?

android:onClick="/*your method name*/"

If so, make sure that there are no typos. In your case it would probably be:

android:onClick="addOne"

OR

You can also listen for the click events programmatically. like below:

set the Button in OnCreate method:

var goToRecoverBtn = findViewById(R.id./*id name here*/)

after that add the ClickListener to the goToRecoverBtn:

goToRecoverBtn.setOnClickListener{
         // do anything whatever you want
}

OR

MainActivity code looks like below

class MainActivity : AppCompatActivity() {

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

        var goToRecoverBtn = findViewById(R.id./*id name here*/)

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


    }
}

BTW could you please show us your .xml file?

brew
  • 91
  • 2
  • 11