【发布时间】:2019-07-18 04:04:42
【问题描述】:
所以我尝试在片段Home 中创建退出按钮,但是,每次用户退出时,应用程序都会崩溃并且它是由文档快照引起的。
这是我的文档快照代码。
private fun loadProfile() {
val user = mAuth!!.currentUser
if (user != null) {
...//loading profile code
}
val uid = user.uid
documentReference = db!!.collection("users").document(uid)
documentReference!!.addSnapshotListener { documentSnapshot, e ->
//this if returning null after user signout
if (documentSnapshot!!.exists()) {
if (documentSnapshot.get(KEY_HP) != null) {
...//loading profile code
}else {
val intent = Intent(this@Home.activity, LoginActivity::class.java)
startActivity(intent)
}
}
} else {
val intent = Intent(this@Home.activity, LoginActivity::class.java)
startActivity(intent)
}
}
如您所见,我拼命地在任何地方添加else,但这无济于事。
这是我的退出方法
private fun signOut() {
mAuth!!.signOut()
val intent = Intent(this@Home.activity, LoginActivity::class.java)
startActivity(intent)
}
所以,我需要在应用程序不崩溃的情况下正确退出
【问题讨论】:
-
如果应用程序崩溃,会有一个堆栈跟踪。请在 logcat 上查找,并将其添加到您的问题中。
-
我已经查过LogCat,这就是我发现
if (documentSnapshot!!.exists())在退出后返回null并导致应用程序崩溃, -
实际的崩溃是什么?将该消息添加到您的问题中。
-
也已经写好了,崩溃是由documentSnapshot返回null引起的
标签: android firebase kotlin google-cloud-firestore firebase-authentication