PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, September 4, 2022

[FIXED] When logging in in login and register modes, leaving the edittexts blank, it kicks them out of the application

 September 04, 2022     android, android-studio, authentication, java, sign     No comments   

Issue

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

            }
        });
    }
}```

Solution

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


Answered By - Gobu CSG
Answer Checked By - Senaida (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing