我正在使用 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/