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

Kotlin开发Android聊天应用:Firebase Auth创建用户失败求助

Firebase注册失败:数据无法上传,提示"Some error occurred"

我用Kotlin开发Android聊天应用,包含注册(Signup.kt)和登录功能。现在注册时邮箱密码无法上传到Firebase,只会弹出"Some error occurred"的Toast,调试没解决。相关代码如下:

Signup.kt

class SignUp : AppCompatActivity() {
    private lateinit var mAuth: FirebaseAuth

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_sign_up)
        supportActionBar?.hide()

        mAuth = FirebaseAuth.getInstance()

        btnSignup.setOnClickListener{
            val email = edt_email.text.toString()
            val password = edt_pwd.text.toString()

            signup(email, password)
        }
    }

    private fun signup(email: String, password: String){
        mAuth.createUserWithEmailAndPassword(email, password)
            .addOnCompleteListener(this) { task ->
                if (task.isSuccessful) {
                    val intent = Intent(this@SignUp, MainActivity::class.java)
                    startActivity(intent)
                } else {
                    Toast.makeText(this@SignUp, "Some error occured", Toast.LENGTH_SHORT).show()
                }
            }
    }
}

activity_sign_up.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".SignUp">

    <ImageView
        android:id="@+id/app_logo"
        android:layout_width="150dp"
        android:layout_height="150dp"
        android:src="@drawable/whatsapp"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="70dp"/>

    <EditText
        android:id="@+id/edt_name"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_below="@+id/app_logo"
        android:layout_marginTop="10dp"
        android:background="@drawable/edt_background"
        android:hint="Name"
        android:paddingLeft="13dp"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"/>

    <EditText
        android:id="@+id/edt_email"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_below="@+id/edt_name"
        android:layout_marginTop="10dp"
        android:paddingLeft="13dp"
        android:hint="Email"
        android:background="@drawable/edt_background"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"/>

    <EditText
        android:id="@+id/edt_pwd"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_below="@+id/edt_email"
        android:layout_marginTop="10dp"
        android:background="@drawable/edt_background"
        android:hint="Password"
        android:paddingLeft="13dp"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"/>

    <Button
        android:id="@+id/btnSignup"
        android:layout_width="150dp"
        android:layout_height="wrap_content"
        android:layout_below="@+id/edt_pwd"
        android:text="Sign Up"
        android:background="@drawable/btn_background"
        android:textColor="@color/white"
        android:textSize="20sp"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="10dp"/>

</RelativeLayout>

排查与修复方案

1. 先获取具体错误信息

当前Toast只显示通用错误,完全无法定位问题根源。修改signup方法,直接显示Firebase返回的具体错误内容:

private fun signup(email: String, password: String){
    mAuth.createUserWithEmailAndPassword(email, password)
        .addOnCompleteListener(this) { task ->
            if (task.isSuccessful) {
                val intent = Intent(this@SignUp, MainActivity::class.java)
                startActivity(intent)
            } else {
                // 替换通用提示为具体错误信息
                val errorMessage = task.exception?.message ?: "未知错误"
                Toast.makeText(this@SignUp, errorMessage, Toast.LENGTH_LONG).show()
            }
        }
}

运行后就能知道具体是邮箱格式错误、密码长度不足,还是Firebase配置问题。

2. 检查Firebase基础配置

  • 确认项目app目录下已添加google-services.json,且文件内的包名和Firebase控制台项目的包名完全一致
  • 登录Firebase控制台,进入Authentication > 登录方法,确认「邮箱/密码」登录方式已启用

3. 添加输入合法性校验

Firebase对注册有基础规则:邮箱格式必须合法,密码至少6位。在调用注册接口前先做校验:

btnSignup.setOnClickListener{
    val email = edt_email.text.toString().trim()
    val password = edt_pwd.text.toString().trim()

    // 校验邮箱
    if (email.isEmpty()) {
        edt_email.error = "请输入邮箱"
        edt_email.requestFocus()
        return@setOnClickListener
    }
    if (!android.util.Patterns.EMAIL_ADDRESS.matcher(email).matches()) {
        edt_email.error = "请输入合法邮箱地址"
        edt_email.requestFocus()
        return@setOnClickListener
    }

    // 校验密码
    if (password.isEmpty()) {
        edt_pwd.error = "请输入密码"
        edt_pwd.requestFocus()
        return@setOnClickListener
    }
    if (password.length < 6) {
        edt_pwd.error = "密码长度不能少于6位"
        edt_pwd.requestFocus()
        return@setOnClickListener
    }

    signup(email, password)
}

4. 确认网络权限

检查AndroidManifest.xml中是否添加了网络权限:

<uses-permission android:name="android.permission.INTERNET" />

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.17 12:15:46