You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Kotlin函数仅部分生效:登录成功日志输出但后续日志无输出?

Firebase登录成功后后续日志无输出的排查

问题描述

代码执行到Log.d(TAG, "signInWithCredential:success")时,Logcat能正常输出该日志,但紧随其后的Log.d("tag", "User: $user")及后续所有日志均无输出,相关代码如下:

private fun signInWithPhoneAuthCredential(credential: PhoneAuthCredential) {
    mAuth.signInWithCredential(credential)
        .addOnCompleteListener(this) { task ->
            val user = task.result?.user // Declare and initialize the user variable

            if (task.isSuccessful) {
                // Sign in success, update UI with the signed-in user's information
                Log.d(TAG, "signInWithCredential:success")

                Log.d("tag", "User: $user") // Log the user value here

                if (user != null) {
                    Log.d("tag", "User is not null")
                    val userId: String = user.uid
                    val users = Users(userId, "", "${user.phoneNumber}", "", "", "", "", "", "", "")

                    firestore.collection("Users").document("UserInfo").collection(userId)
                        .add(users)
                        .addOnSuccessListener {
                            Log.d("tag", "Database operation success")
                            startActivity(Intent(applicationContext, SetUserInfoActivity::class.java))
                            progressDialog.dismiss()
                        }
                        .addOnFailureListener { e ->
                            Log.d("tag", "Database operation failed: ${e.message}")
                            progressDialog.dismiss()

                        }
                } else {
                    Log.d("tag", "User is null")
                    Toast.makeText(applicationContext, "Something went wrong", Toast.LENGTH_SHORT).show()
                }

            } else {
                // Sign in failed, display a message and update the UI
                progressDialog.dismiss()
                Log.w(TAG, "signInWithCredential:failure", task.exception)
                if (task.exception is FirebaseAuthInvalidCredentialsException) {
                    // The verification code entered was invalid
                }
                // Update UI
            }
        }
}

可能原因及解决方法

  • Logcat过滤规则限制:检查Logcat的过滤条件,是否仅设置了显示TAG标签的日志,导致tag标签的日志被过滤。可以清空过滤条件,或直接搜索tag关键词查看对应日志。

  • 日志代码抛出未捕获异常:虽然登录成功,但打印User: $user时可能因user对象的toString()方法异常、或变量解析错误导致代码中断。可以用try-catch包裹日志代码排查:

    try {
        Log.d("tag", "User: $user")
    } catch (e: Exception) {
        Log.e("tag", "打印用户信息出错: ${e.message}", e)
    }
    
  • 页面跳转提前终止进程:如果startActivity执行后当前Activity被快速销毁,可能后续日志还未完成输出就被终止。可以暂时注释掉startActivity和progressDialog.dismiss(),验证日志是否正常输出。

  • Firestore操作阻塞主线程:Firestore的写入操作如果在UI线程执行,可能导致主线程卡顿,日志输出被延迟或中断。可以将Firestore操作移至后台线程执行,例如使用Coroutine:

    if (user != null) {
        Log.d("tag", "User is not null")
        val userId: String = user.uid
        val users = Users(userId, "", "${user.phoneNumber}", "", "", "", "", "", "", "")
    
        CoroutineScope(Dispatchers.IO).launch {
            try {
                firestore.collection("Users").document("UserInfo").collection(userId)
                    .add(users)
                withContext(Dispatchers.Main) {
                    Log.d("tag", "Database operation success")
                    startActivity(Intent(applicationContext, SetUserInfoActivity::class.java))
                    progressDialog.dismiss()
                }
            } catch (e: Exception) {
                withContext(Dispatchers.Main) {
                    Log.d("tag", "Database operation failed: ${e.message}")
                    progressDialog.dismiss()
                }
            }
        }
    }
    
  • 变量解析异常:尝试直接打印user的具体属性(如uid、phoneNumber),避免调用整个对象的toString()方法,排查是否是对象序列化问题:

    Log.d("tag", "用户UID: ${user?.uid}, 手机号: ${user?.phoneNumber}")
    

内容的提问来源于stack exchange,提问作者Murad Axundov

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.23 02:37:36