Kotlin / Android Studio - 当电子邮件地址格式错误时,应用程序在注册时崩溃[重复]

Posted

技术标签:

【中文标题】Kotlin / Android Studio - 当电子邮件地址格式错误时,应用程序在注册时崩溃[重复]【英文标题】:Kotlin/Android Studio - App crashing on register when e-mail address is badly formatted [duplicate] 【发布时间】:2019-08-03 12:16:08 【问题描述】:

抱歉 - 我对 Kotlin 和 android sdk 非常陌生,但我希望这意味着这将是一个容易解决的问题!

在我的应用程序使用注册页面时,每次我尝试使用无效的电子邮件地址(例如:“test”)注册时,应用程序都会崩溃,我已经创建了一个 .addOnFailureListener(在底部可见我的代码如下) - 但我仍然收到一条错误消息,提示“电子邮件地址格式错误”。在我的“Toast.makeText”有机会向用户解决问题之前,我的应用程序似乎崩溃了。如果电子邮件地址无效,有什么方法可以完全停止注册?还是有其他方法可以解决这个问题?

override fun onCreate(savedInstanceState: Bundle?) 
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)

    register_button_register.setOnClickListener 
        performRegister()
    

    already_have_account_text_view.setOnClickListener 
        Log.d("MainActivity", "Try to show login activity")

        val intent = Intent(this, LoginActivity::class.java)
        startActivity(intent)
    



private fun performRegister() 
    val email = email_edittext_register.text.toString()
    val password = password_edittext_register.text.toString()

    if (email.isEmpty() || password.isEmpty()) 
        Toast.makeText(this, "Please enter your email and password", Toast.LENGTH_SHORT).show()
        return
    

    Log.d("MainActivity", "Email is: " + email)
    Log.d("MainActivity", "Password: $password")

    FirebaseAuth.getInstance().createUserWithEmailAndPassword(email, password)
        .addOnCompleteListener 
            if (it.isSuccessful) return@addOnCompleteListener

            Log.d("Main", "Successfully created user with uid: $it.result.user.uid")
        
        .addOnFailureListener
            Log.d("Main", "Failed to create user: $it.message")
            Toast.makeText(this, "Please enter a valid email and password", Toast.LENGTH_SHORT).show()
        


这是我面临的错误消息:

2019-03-12 22:04:48.112 29082-29082/com.example.messenger E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.messenger, PID: 29082
    com.google.android.gms.tasks.RuntimeExecutionException: com.google.firebase.auth.FirebaseAuthInvalidCredentialsException: The email address is badly formatted.
        at com.google.android.gms.tasks.zzu.getResult(Unknown Source:21)
        at com.example.messenger.MainActivity$performRegister$1.onComplete(MainActivity.kt:51)
        at com.google.android.gms.tasks.zzj.run(Unknown Source:23)
        at android.os.Handler.handleCallback(Handler.java:873)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:6669)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
     Caused by: com.google.firebase.auth.FirebaseAuthInvalidCredentialsException: The email address is badly formatted.
        at com.google.firebase.auth.api.internal.zzce.zzb(Unknown Source:223)
        at com.google.firebase.auth.api.internal.zzbb.zza(Unknown Source:42)
        at com.google.firebase.auth.api.internal.zzcy.zzc(Unknown Source:11)
        at com.google.firebase.auth.api.internal.zzdb.onFailure(Unknown Source:35)
        at com.google.firebase.auth.api.internal.zzci.dispatchTransaction(Unknown Source:83)
        at com.google.android.gms.internal.firebase_auth.zzb.onTransact(Unknown Source:22)
        at android.os.Binder.execTransact(Binder.java:731)

2019-03-12 22:04:48.114 1882-2869/? W/ActivityManager:   Force finishing activity com.example.messenger/.MainActivity

2019-03-12 22:04:48.149 1882-1955/? W/InputDispatcher: channel '37860b6 com.example.messenger/com.example.messenger.MainActivity (server)' ~ Consumer closed input channel or an error occurred.  events=0x9

2019-03-12 22:04:48.149 1882-1955/? E/InputDispatcher: channel '37860b6 com.example.messenger/com.example.messenger.MainActivity (server)' ~ Channel is unrecoverably broken and will be disposed!

2019-03-12 22:04:48.154 1882-4213/? I/WindowManager: WIN DEATH: Window37860b6 u0 com.example.messenger/com.example.messenger.MainActivity

2019-03-12 22:04:48.154 1882-4213/? W/InputDispatcher: Attempted to unregister already unregistered input channel '37860b6 com.example.messenger/com.example.messenger.MainActivity (server)'

2019-03-12 22:04:48.160 1723-1747/? W/SurfaceFlinger: Attempting to set client state on removed layer: com.example.messenger/com.example.messenger.MainActivity#0

2019-03-12 22:04:48.160 1723-1747/? W/SurfaceFlinger: Attempting to destroy on removed layer: com.example.messenger/com.example.messenger.MainActivity#0

2019-03-12 22:04:48.164 1723-1776/? W/SurfaceFlinger: Attempting to destroy on removed layer: AppWindowToken4e2ac9 token=Token5d60ad0 ActivityRecord626da93 u0 com.example.messenger/.MainActivity t37#0

2019-03-12 22:04:49.332 27898-27898/? I/Choreographer: Skipped 34 frames!  The application may be doing too much work on its main thread.

2019-03-12 22:04:49.426 1882-1936/? I/Choreographer: Skipped 39 frames!  The application may be doing too much work on its main thread.

2019-03-12 22:04:49.426 2609-2609/? I/Choreographer: Skipped 41 frames!  The application may be doing too much work on its main thread.

2019-03-12 22:04:49.439 2021-2021/? I/Choreographer: Skipped 55 frames!  The application may be doing too much work on its main thread.

任何人都可以理解这一点吗?抱歉,我知道这个错误很长,但也许它对外面的人来说意味着什么。

【问题讨论】:

【参考方案1】:

在发送之前验证电子邮件字符串是否为有效电子邮件,例如使用this method

【讨论】:

不要发布仅链接到另一个答案的答案,而是将flag the question作为副本发布。

以上是关于Kotlin / Android Studio - 当电子邮件地址格式错误时,应用程序在注册时崩溃[重复]的主要内容,如果未能解决你的问题,请参考以下文章

Android@Kotlin 在Android studio 中配置Kotlin

Android@Kotlin 在Android studio 中配置Kotlin

android studio怎么用kotlin

Google Android Studio Kotlin

Android Studio配置Kotlin开发环境

android studio 怎么使用kotlin