字符串为 NULL 但 IsNullOrEmpty 函数不起作用

String is NULL but IsNullOrEmpty function not working

我正在尝试对应用程序进行身份验证,我的代码正在为用户变量打印出 NULL。但是,当应用程序运行时,它将变为 Log.e("Execution","2") 而不是 Log.e("Execution","1")。为什么是这样?由于用户变量为空,它应该转到第 1 节。

//Authentication:
        var auth: FirebaseAuth = FirebaseAuth.getInstance()
        var user: String = auth.currentUser.toString()
        Log.e("User", user)

        //*EXECUTION BEGIN*
        //If user is NOT logged in:
        if (user.isNullOrEmpty()) {
            Log.e("Execution","1")

            //Do a fresh open (Splash -> Login -> Groups)
            var intent = Intent(this, SplashActivity::class.java)
            startActivity(intent)
            Log.e("Execution","1a")
            finish() //Check to see if this stops Main Activity
            Log.e("Execution","1b")

            Handler().postDelayed({
                val intent = Intent(this, GroupsActivity::class.java)
                startActivity(intent)
                finish()
            }, splashdelay)
            setContentView(R.layout.activity_login_signup)

        } else { //User is logged in:
            Log.e("Execution","2")

            //If the user is starting up the app from a notif or another direct target screen,
            //load the appropriate screen.

            //Otherwise, go directly to the Groups screen
            setContentView(R.layout.activity_groups)

        }

如果您在空对象上调用 toString(),它会 returns 字符串“null”。

因此,不要使用 auth.currentUser.toString(),只需使用 auth.currentUser 并在您的 if 条件下,检查 if (user != null) 使用的是 to check if a user is signed in