Android Kotlin Firebase addOnCompleteListener showing error

Viewed 1300

I'm using Kotlin and calling a Firebase Auth API:

private fun loginUser() {
    email = etEmail?.text.toString()
    password = etPassword?.text.toString()

    mAuth!!.createUserWithEmailAndPassword(email!!, password!!)
            .addOnCompleteListener(this) { task ->
                if (task.isSuccessful) {
                    // Sign in success, update UI with the signed-in user's information
                    println("createUserWithEmail:success")
                    val user = mAuth?.currentUser
                    updateUI()
                } else {
                    // If sign in fails, display a message to the user.
                    Log.w("TAG", "createUserWithEmail:failure", task.exception)
                    println("Authentication failed.")
                    updateUI()
                }
            }

}

The .addOnCompleteListener(this) line is showing these problems:

None of the following functions can be called with the arguments supplied: 
public open fun addOnCompleteListener(p0: Activity, p1: OnCompleteListener<AuthResult!>): Task<AuthResult!> defined in com.google.android.gms.tasks.Task
public open fun addOnCompleteListener(p0: Executor, p1: OnCompleteListener<AuthResult!>): Task<AuthResult!> defined in com.google.android.gms.tasks.Task

I'm doing this in a Fragmet. I've done this in an Activity before. I compare the code and setup with that one and it is all the same. Not sure why it is showing the error.

My Firebase db has login with email and password enabled.

My AndroidManifest.xml file has Internet enabled.

My app is setup correctly with Firebase.

Not sure what I'm missing.

Thanks for any help.

2 Answers

Here is the answer for copy&paste. If you do this in an Fragment, instead in an activity like the example did, you have to use requireActivity(). The Function requires the activity as parameter. But with using this as parameter you passed the fragment you are in.

private fun loginUser() {
    email = etEmail?.text.toString()
    password = etPassword?.text.toString()

    mAuth!!.createUserWithEmailAndPassword(email!!, password!!)
            .addOnCompleteListener(requireActivity()) { task ->  // <<< CHANGE WAS MADE HERE !
                if (task.isSuccessful) {
                    // Sign in success, update UI with the signed-in user's information
                    println("createUserWithEmail:success")
                    val user = mAuth?.currentUser
                    updateUI()
                } else {
                    // If sign in fails, display a message to the user.
                    Log.w("TAG", "createUserWithEmail:failure", task.exception)
                    println("Authentication failed.")
                    updateUI()
                }
            }

}

If your code is in a Fragment, that means this probably refers to that Fragment. The error message is helpfully telling you that this is neither an Activity, nor an Executor as required. You can't pass a Fragment instance.

You could pass an Activity instance instead using getActivity().

Related