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

Firebase注册时出现邮箱格式错误异常求助(附Kotlin代码)

问题解决:Firebase Auth注册时邮箱格式错误异常

核心原因

你的代码中获取用户输入的方式完全错误:你直接对TextInputLayout组件调用toString(),得到的是组件对象的字符串表示(比如com.google.android.material.textfield.TextInputLayout{...}),而不是用户输入的邮箱、姓名等内容。这种无效字符串传递给Firebase Auth时,自然会触发邮箱格式错误的异常。

修正方案

需要获取TextInputLayout内部嵌套的TextInputEditText的文本内容,有两种可行方式:

方式1:通过TextInputLayout的editText属性获取

直接利用TextInputLayout的editText属性拿到内部输入框,再提取文本:

binding.tvRegister.setOnClickListener {
    val dialog = BottomSheetDialog(this@LoginActivity)
    val view = layoutInflater.inflate(R.layout.bottom_sheet_layout, null)

    dialog.setContentView(view)

    // 修正输入内容获取方式
    val tilName = view.findViewById<TextInputLayout>(R.id.etName)
    val etName = tilName.editText?.text.toString().trim()
    
    val tilSurname = view.findViewById<TextInputLayout>(R.id.etSurname)
    val etSurname = tilSurname.editText?.text.toString().trim()
    
    val tilMail = view.findViewById<TextInputLayout>(R.id.etRegisterEmail)
    val etMail = tilMail.editText?.text.toString().trim()
    
    val tilPassword = view.findViewById<TextInputLayout>(R.id.etRegisterPassword)
    val etPassword = tilPassword.editText?.text.toString().trim()
    
    val tilHeight = view.findViewById<TextInputLayout>(R.id.etHeight)
    val etHeight = tilHeight.editText?.text.toString().trim()
    
    val tilWeight = view.findViewById<TextInputLayout>(R.id.etWeight)
    val etWeight = tilWeight.editText?.text.toString().trim()

    val btnRegister = view.findViewById<Button>(R.id.btnRegister)

    btnRegister.setOnClickListener {
        // 空值判断也需要修正,确保输入内容有效
        if (etName.isNotEmpty() && etSurname.isNotEmpty() && etMail.isNotEmpty() && etPassword.isNotEmpty()) {
            val user = hashMapOf(
                "name" to etName,
                "surname" to etSurname,
                "email" to etMail,
                "password" to etPassword,
                "height" to etHeight,
                "weight" to etWeight
            )
            registerUser(etMail, etPassword, user)
        } else {
            Toast.makeText(
                this@LoginActivity,
                "请填写所有必填项",
                Toast.LENGTH_SHORT
            ).show()
        }
    }
    dialog.show()
}

方式2:给TextInputEditText设置id直接获取

在bottom_sheet_layout.xml中,给每个TextInputEditText添加id,比如:

<com.google.android.material.textfield.TextInputEditText
    android:id="@+id/etNameInput"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:inputType="textPersonName"
    android:textColorHint="@color/primaryDarkColor" />

然后在代码中直接通过id获取输入框文本:

val etName = view.findViewById<TextInputEditText>(R.id.etNameInput).text.toString().trim()

额外优化建议

  1. 邮箱输入框类型修正:在bottom_sheet_layout.xml中,邮箱输入框的inputType应该改为textEmailAddress,这样系统会自动验证邮箱格式,提前拦截无效输入:
<com.google.android.material.textfield.TextInputEditText
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:inputType="textEmailAddress"  <!-- 修改这里 -->
    android:textColorHint="@color/secondaryDarkColor" />
  1. 协程与回调混用问题:你的registerUser函数中同时使用了await()和addOnSuccessListener,这会导致代码逻辑混乱。建议统一用协程风格:
private fun registerUser(email: String, password: String, user: HashMap<String, String>) {
    CoroutineScope(Dispatchers.IO).launch {
        try {
            // 用await()获取注册结果,无需回调
            val authResult = auth.createUserWithEmailAndPassword(email, password).await()
            val currentUser = authResult.user
            currentUser?.let {
                db.document(it.email.toString()).set(user).await()
                withContext(Dispatchers.Main) {
                    Toast.makeText(this@LoginActivity, "欢迎", Toast.LENGTH_LONG).show()
                    checkLogged()
                }
            }
        } catch (e: Exception) {
            withContext(Dispatchers.Main){
                Toast.makeText(this@LoginActivity, e.message, Toast.LENGTH_LONG).show()
            }
        }
    }
}
  1. 空值安全处理:获取editText时可能为空,建议添加空值判断,避免潜在的空指针异常。

内容的提问来源于stack exchange,提问作者Mert Ozan

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.09 17:55:29