【问题标题】:In Android Kotlin App Dev, Firebase says The email address is badly formatted在 Android Kotlin App Dev 中,Firebase 说电子邮件地址格式错误
【发布时间】:2020-06-07 10:19:28
【问题描述】:

这是我的 Kotlin 活动代码:

package com.florize.nostagram

import android.content.Intent
import android.os.Bundle
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.google.firebase.auth.FirebaseAuth
import com.google.firebase.auth.FirebaseUser
import kotlinx.android.synthetic.main.activity_login.*

class LoginActivity : AppCompatActivity() {
    var auth: FirebaseAuth? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_login)
        auth = FirebaseAuth.getInstance()
        email_login_button.setOnClickListener {
            signinAndSignup()
        }
    }

    fun signinAndSignup() {
        auth?.createUserWithEmailAndPassword(
            email_edittext.toString().trim(),
            password_edittext.toString()
        )
            ?.addOnCompleteListener { task ->
                if (task.isSuccessful) {
                    //Creating a user account
                    moveMainPage(task.result?.user)
                } else if (task.exception?.message.isNullOrEmpty()) {
                    //Show the error message
                    Toast.makeText(this, task.exception?.message, Toast.LENGTH_LONG).show()
                } else {
                    //Login if you have account
                    signinEmail()
                }
            }
    }
}

和我的布局代码:

<com.google.android.material.textfield.TextInputLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="20dp"
            android:layout_marginRight="20dp">

            <EditText
                android:id="@+id/email_edittext"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:inputType="textEmailAddress"
                android:hint="@string/email" />

        </com.google.android.material.textfield.TextInputLayout>

我为电子邮件字段设置了 inputType。 但 Firebase Toast 说“电子邮件地址格式错误。”。 我在“email_edittext.toString()”后面添加了 trim() 不起作用。仍然是同样的错误。 我该如何解决?

(我剪掉了我的 Kotlin 活动代码,因为它太长了)

【问题讨论】:

    标签: android firebase kotlin firebase-authentication


    【解决方案1】:

    改变这个:

    auth?.createUserWithEmailAndPassword(
                email_edittext.toString().trim(),
                password_edittext.toString()
            )
    

    进入这个:

    auth?.createUserWithEmailAndPassword(
                email_edittext.text.toString().trim(),
                password_edittext.text.toString()
            )
    

    您需要使用text 属性来访问textfield 的值

    【讨论】:

      猜你喜欢
      • 2018-02-18
      • 2017-02-01
      • 1970-01-01
      • 2018-09-24
      • 2021-01-03
      • 2020-02-27
      • 2022-12-04
      • 2018-09-09
      • 1970-01-01
      相关资源
      最近更新 更多