Android Kotlin Firebase addOnCompleteListener 显示错误

标签 android firebase kotlin fragment

我正在使用 Kotlin 并调用 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()
                }
            }

}

.addOnCompleteListener(this) 行显示了这些问题:

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

我正在 Fragmet 中执行此操作。我以前在 Activity 中做过这个。我将代码和设置与那个进行比较,结果完全相同。不确定为什么会显示错误。

我的 Firebase 数据库启用了使用电子邮件和密码登录。

我的 AndroidManifest.xml 文件启用了 Internet。

我的应用已通过 Firebase 正确设置。

不确定我错过了什么。

感谢您的帮助。

最佳答案

这里是复制粘贴的答案。 如果您在 Fragment 中执行此操作,而不是像示例那样在 Activity 中执行此操作,则必须使用 requireActivity()。 该功能需要 Activity 作为参数。但是使用 this 作为参数,你传递了你所在的 fragment 。

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()
                }
            }

}

关于Android Kotlin Firebase addOnCompleteListener 显示错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65817683/

相关文章:

android - 使用android中的警报框弹出窗口从gridview更新数据库

android - VIEW PAGER 如何更改 View 堆栈中的页面顺序

flutter - 来自原生android的Channel Invokemethod不调用flutter方法

android - 当我在我的第一个 fragment 上按下后退按钮时,应用程序应该退出

java - 在 ACTION_DOWN 触摸事件中使用 Android 计时器错误

java - FirebaseRecyclerAdapter 不会覆盖 onDataChanged

javascript - 在 Firebase 中,我收到未捕获的类型错误 : Cannot read property 'initializeApp' of undefined,,但不确定为什么未定义 'firebase'?

ios - 如何获取Firebase云存储中存储的项目的总大小?

android - 未能解决 glide,现在 3rd-party Gradle 插件可能是原因

kotlin - 我可以在when语句中进行smartCast吗?