Home > OS >  When logging in in login and register modes, leaving the edittexts blank, it kicks them out of the a
When logging in in login and register modes, leaving the edittexts blank, it kicks them out of the a

Time:07-21

I'm new to Android, there are 2 modes in the code I wrote (so we can think of it). When I leave the edittexts blank in the registration section, there is no problem, but when I leave the edittexts blank in the mode I am trying to log in, it throws them out of the application.

step by step:

Step 1: registration mode > leaving e-mail password and username blank, no problem

step 2: login mode > leaving email and password blank will throw an error and kick you out of the app

this is my error message: java.lang.IllegalArgumentException: Given String is empty or null

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_giris);

        username = (EditText) findViewById(R.id.kullaniciadi);
        password = (EditText) findViewById(R.id.sifre);
        eposta = (EditText) findViewById(R.id.eposta);

        button = (Button) findViewById(R.id.giris);

        login = (TextView) findViewById(R.id.Logininfo);
        forgotpas = (TextView) findViewById(R.id.forgotpas);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(eposta.getText().toString().isEmpty() || password.getText().toString().isEmpty()) {
                    //sign up modu
                    if(signup && username.getText().toString().isEmpty()){
                        Toast.makeText(Giris_activity.this, "Invalid input", Toast.LENGTH_SHORT).show();
                        return;
                        //eğer eposta ve şifre doğruysa gir---eğer signup modundaysak username de doğruysa gir
                    }
                }

                if(signup)
                {
                    handlesignup();
                }else
                {
                    handlelogin();
                }
            }
        });

        forgotpas.setVisibility(View.GONE);
        login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (signup) {
                    signup = false;
                    username.setVisibility(View.GONE);
                    forgotpas.setVisibility(View.VISIBLE);
                    button.setText("Log in");
                    login.setText("Dont have an account? Sign up");
                } else {
                    signup = true;
                    username.setVisibility(View.VISIBLE);
                    forgotpas.setVisibility(View.GONE);
                    button.setText("Sign up");
                    login.setText("Already have an account? Log in");
                }
            }
        });

        forgotpas.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                openforgotpas();
            }
        });
    }

    //-----------------------firebase---------------------------
    private  void handlesignup() {
        FirebaseAuth.getInstance().createUserWithEmailAndPassword(eposta.getText().toString(),password.getText().toString()).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
            @Override
            public void onComplete(@NonNull Task<AuthResult> task) {
                if(task.isSuccessful())
                {
                    Toast.makeText(Giris_activity.this, "Signed up successfully, you can login", Toast.LENGTH_SHORT).show();
                }else{
                    Toast.makeText(Giris_activity.this, task.getException().getLocalizedMessage(), Toast.LENGTH_LONG).show();
                }
                    
            }
        });

    }

    private void handlelogin() {
        FirebaseAuth.getInstance().signInWithEmailAndPassword(eposta.getText().toString(),password.getText().toString()).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
            @Override
            public void onComplete(@NonNull Task<AuthResult> task) {
                if(task.isSuccessful())
                {
                    Toast.makeText(Giris_activity.this, "Logged in successfully", Toast.LENGTH_SHORT).show();
                    openbaslangic();
                }else{
                    Toast.makeText(Giris_activity.this, task.getException().getLocalizedMessage(), Toast.LENGTH_LONG).show();
                }

            }
        });
    }
}```

CodePudding user response:

leaving e-mail password and username blank, no problem

There is no crash because you have a partial validation (although I'm not sure why it works only for one field and only for the signing up proccess)

if (signup && username.getText().toString().isEmpty()) { return; }

leaving email and password blank will throw an error and kick you out of the app

You're not validating any fields here and calling signInWithEmailAndPassword() with empty strings will cause a crash which comes from Firebase SDK.

CodePudding user response:

Modify button click like this

You've to validate before submitting firebase edit text values. User name optional but when you read it's possible to throw null pointer exception

button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(eposta.getText().toString().isEmpty() || password.getText().toString().isEmpty()) {
                    //sign up modu
                    return;
                }
               if(signup && username.getText().toString().isEmpty()){
                        Toast.makeText(Giris_activity.this, "Invalid input", Toast.LENGTH_SHORT).show();
                        return;
                        //eğer eposta ve şifre doğruysa gir---eğer signup modundaysak username de doğruysa gir
               }

                if(signup){
                    handlesignup();
                }else{
                    handlelogin();
                }
            }
        });
  • Related