1

Sorry - I'm very new to Kotlin and Android sdk, but I'm hoping that means this will be an easy question to solve!

When using the register page for my app, the app crashes every time I try to register using an invalid e-mail address (For example: "test"), I have already created an .addOnFailureListener (visible at the bottom of my code below) - but I still receive an error saying "the email address is badly formatted". My app seems to crash before my "Toast.makeText" has a chance to address the problem to the user. Is there a way I can stop the register completely if the email address is not a valid one? Or is there another way I can get around this issue?

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()
        }

}

this is the error message I'm facing:

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: Window{37860b6 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: AppWindowToken{4e2ac9 token=Token{5d60ad0 ActivityRecord{626da93 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.

Can anyone make sense of this? Sorry I know that error is a long one, but maybe it means something to someone out there.

Cœur
  • 37,241
  • 25
  • 195
  • 267

1 Answers1

0

Validate if email string is a valid email before sending it, for instance using this method

Kuba Pawłowski
  • 365
  • 2
  • 10
  • 1
    Instead of posting an answer which merely links to another answer, please instead [flag the question](https://stackoverflow.com/help/privileges/flag-posts) as a duplicate. – Zoe Mar 12 '19 at 22:31