Android - Firebase - The email address is badly formatted

柔情痞子 提交于 2019-12-13 15:04:33

问题


Aim

Allow users to register their preferred display name, home address, email, and password by utilizing Firebase Authentication (Email and Password)

Java Class

import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.design.widget.TextInputLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseAuthException;

public class RegistrationActivity extends AppCompatActivity {

    private TextInputLayout jRegisterName;
    private TextInputLayout jRegisterAddress;
    private TextInputLayout jRegisterEmail;
    private TextInputLayout jRegisterPassword;
    private Button jRegisterRegBtn;

    //Firebase
    private FirebaseAuth mAuth;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_registration);

        //Firebase Auth
        mAuth = FirebaseAuth.getInstance();

        jRegisterName = (TextInputLayout) findViewById(R.id.registerName);
        jRegisterAddress = (TextInputLayout) findViewById(R.id.registerAddress);
        jRegisterEmail = (TextInputLayout) findViewById(R.id.registerEmail);
        jRegisterPassword = (TextInputLayout) findViewById(R.id.registerPassword);
        jRegisterRegBtn = (Button) findViewById(R.id.registerRegBtn);

        jRegisterRegBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                String userDisplayName = jRegisterName.getEditText().toString();
                String userHomeAddress = jRegisterAddress.getEditText().toString();
                String userEmail = jRegisterEmail.getEditText().toString().trim();
                String userPassword = jRegisterPassword.getEditText().toString().trim();

                registerUser(userDisplayName, userHomeAddress, userEmail, userPassword);

            }

            private void registerUser(String userDisplayName, String userHomeAddress, String userEmail, String userPassword) {
                mAuth.createUserWithEmailAndPassword(userEmail, userPassword).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
                            @Override
                            public void onComplete(@NonNull Task<AuthResult> task) {
                                if (task.isSuccessful()) {
                                    Intent intentMain = new Intent(RegistrationActivity.this, MainActivity.class);
                                    startActivity(intentMain);
                                    finish();
                                }else if(!task.isSuccessful()){
                                    FirebaseAuthException e = (FirebaseAuthException )task.getException();
                                    Toast.makeText(RegistrationActivity.this, "Failed Registration: "+e.getMessage(), Toast.LENGTH_SHORT).show();
                                    return;
                                }
                            }
                        });
            }
        });

    }
}

Problem

After the user has entered the input such as

Name : JohnSmith

HomeAddress : 18 King Street

Email : johnsmith@gmail.com

Password : 123456789pass

The user fails to register and the message below prompts out

"Failed Registration: The email address is badly formatted"


回答1:


Check your inputType on the xml file. There is 3 types of inputs on email.

android:inputType="textWebEmailAddress"
android:inputType="textEmailAddress"
android:inputType="textEmailSubject"

More here https://developer.android.com/reference/android/text/InputType.html




回答2:


Achieve by doing this simply

  • you need to dosetInputType

android:inputType="textEmailAddress"

  • You can done with programmatic also

emailEditText.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS

Happy to help you




来源:https://stackoverflow.com/questions/46139173/android-firebase-the-email-address-is-badly-formatted

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!