Android Kotlin Firebase addOnCompleteListener 显示错误
Android Kotlin Firebase addOnCompleteListener showing error
我正在使用 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。
不确定我错过了什么。
感谢您的帮助。
如果您的代码在片段中,则意味着 this
可能引用该片段。错误消息有助于告诉您 this
既不是 Activity,也不是所需的执行器。您不能传递 Fragment 实例。
您可以传递 Activity 实例,而不是使用 getActivity()。
这里是复制粘贴的答案。
如果您在片段中执行此操作,而不是像示例那样在 activity 中执行此操作,则必须使用 requireActivity()
。
该函数需要 activity 作为参数。但是使用 this
作为参数,你传递了你所在的片段。
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()
}
}
}
我正在使用 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。
不确定我错过了什么。
感谢您的帮助。
如果您的代码在片段中,则意味着 this
可能引用该片段。错误消息有助于告诉您 this
既不是 Activity,也不是所需的执行器。您不能传递 Fragment 实例。
您可以传递 Activity 实例,而不是使用 getActivity()。
这里是复制粘贴的答案。
如果您在片段中执行此操作,而不是像示例那样在 activity 中执行此操作,则必须使用 requireActivity()
。
该函数需要 activity 作为参数。但是使用 this
作为参数,你传递了你所在的片段。
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()
}
}
}