【问题标题】:Logging out of an account with Firebase auth使用 Firebase 身份验证注销帐户
【发布时间】:2021-05-24 02:47:42
【问题描述】:

我正在使用 Kotlin、Android Studio 和 Firebase 构建应用。 我的用户有一个带有 .setOnClickListener 的按钮(如下所示),用于从 Firebase 注销。但是当我打开按钮所在的活动/片段时,应用程序会关闭。

logoutButton.setOnClickListener {
    val intent =
        Intent(activity, AccountFragment::class.java)
    Firebase.firebaseAuth.signOut()
    startActivity(intent)
}

在 Logcat 上没有给我任何错误……至少我看到了。 当我更改为具有按钮的片段时,应用程序关闭并说应用程序已停止。

更新:Logcat 出现错误。它说:

java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)”

这是我更改片段时 logcat 的图像:

Logcat new image

【问题讨论】:

  • 附加调试器并检测何时打开活动片段,逐行检查是否有歧义,或者当您打开片段屏幕时,错误出现在 logcat 中但很快消失,因此你必须滚动 logcat 或突出显示 logcat 中的一些文本,然后打开屏幕,你会发现错误。
  • @Hascher7 我已经找到了错误...谢谢,但我仍然不知道该怎么做..
  • 您在使用数据绑定吗?如果没有,logoutButton 是否已初始化?
  • 你确定logoutButton已经初始化了吗?
  • @AlexMamo 我不确定。但我一定是做错了什么,因为按钮现在出现了,但是当我点击那里时,应用程序关闭了......它说java.lang.IllegalStateException: Could not find method Click(View) in a parent or ancestor Context for android:onClick attribute defined on view class com.google.android.material.button.MaterialButton with id 'bt_logout'

标签: firebase kotlin firebase-authentication


【解决方案1】:

如果按钮在片段内,那么您将 setOnClickListener 代码放在 onViewCreated 而不是 onCreateView 中:

 override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

logoutButton.setOnClickListener {
    val intent =
        Intent(activity, AccountFragment::class.java)
    Firebase.firebaseAuth.signOut()
    startActivity(intent)
}
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-12-04
    • 1970-01-01
    • 2017-12-18
    • 2018-12-29
    • 2016-11-14
    • 1970-01-01
    • 1970-01-01
    • 2013-04-05
    相关资源
    最近更新 更多