0

When I try to register it says registration failed. this is a updated version of the code after some tweaks. I have tried to analyze the code with no luck.

package com.brandshopping.brandshopping;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;

public class RegisterActivity extends AppCompatActivity {

private EditText Input_email;
private EditText Input_password;
private EditText Input_confirm_password;
private EditText Input_first_name;
private EditText Input_last_name;
private EditText Input_phonenumber;
private Button SignUpBtn;
private TextView I_Have_An_Account_Btn;
private ProgressDialog LoadingBar;

private FirebaseAuth firebaseAuth;

public class RegisterActivity extends AppCompatActivity {

private EditText Input_email;
private EditText Input_password;
private EditText Input_confirm_password;
private EditText Input_first_name;
private EditText Input_last_name;
private EditText Input_phonenumber;
private Button SignUpBtn;
private TextView I_Have_An_Account_Btn;
private ProgressDialog LoadingBar;

private FirebaseAuth firebaseAuth;

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


    LoadInGui(); //Loads in GUI


    I_Have_An_Account_Btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            startActivity(new Intent(RegisterActivity.this, MainActivity.class));
        }
    });

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

            final String email = Input_email.getText().toString().trim();
            final String password = Input_password.getText().toString().trim();

            validateCredentialls();
            doNormalLogin(email, password);

        }
    });


}

private void validateCredentialls(){
    if(TextUtils.isEmpty(Input_first_name.toString())){
        Toast.makeText(RegisterActivity.this, "Please enter you're first name", Toast.LENGTH_SHORT).show();
    }

    else if(TextUtils.isEmpty(Input_last_name.toString())){
        Toast.makeText(RegisterActivity.this, "Please enter you're last name", Toast.LENGTH_SHORT).show();
    }

    else if(TextUtils.isEmpty(Input_email.toString())){
        Toast.makeText(RegisterActivity.this, "Please enter you're e-mail", Toast.LENGTH_SHORT).show();
    }

    else if(TextUtils.isEmpty(Input_phonenumber.toString())){
        Toast.makeText(RegisterActivity.this, "Please enter you're phone number", Toast.LENGTH_SHORT).show();
    }

    else if(TextUtils.isEmpty(Input_password.toString())){
        Toast.makeText(RegisterActivity.this, "Please enter you're password", Toast.LENGTH_SHORT).show();
    }

    else if(TextUtils.isEmpty(Input_confirm_password.toString())){
        Toast.makeText(RegisterActivity.this, "Please write you're password again", Toast.LENGTH_SHORT).show();
    }
}

private void doNormalLogin(String email, String password){
    FirebaseAuth mAuth = FirebaseAuth.getInstance();
    mAuth.signInWithEmailAndPassword(email, password).addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
        @Override
        public void onComplete(@NonNull Task<AuthResult> task) {
            if (task.isSuccessful()) {
                Toast.makeText(RegisterActivity.this, "Registration successful", Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(RegisterActivity.this, "Registration failed", Toast.LENGTH_SHORT).show();
                Log.e("SignUpError :", task.getException().getMessage());
            }
        }
    });
}

private void LoadInGui()
{
    Input_email = (EditText) findViewById(R. id. input_email);
    Input_password = (EditText) findViewById(R. id. input_password);
    Input_confirm_password = (EditText) findViewById(R. id. input_confirm_password);
    Input_first_name = (EditText) findViewById(R. id. Input_First_Name);
    Input_last_name = (EditText) findViewById(R. id. Input_Last_Name);
    Input_phonenumber = (EditText) findViewById(R. id. input_phonenumber);

    SignUpBtn = (Button) findViewById(R. id. register_btn);
    I_Have_An_Account_Btn = (TextView) findViewById(R. id. I_Have_An_Account_Btn);

}

}

i expect the program to register me and it doesent. Some help would be very appreciated, thanks in advance.

3
  • it tries to register once opened right because you told it to do: RegisterUser(); in onCreate. To see why it fails, you have to debug your app. I do understand the code i have written is a problem. Don't write the code before you understand what is each part of it going to do. Commented Jun 26, 2019 at 9:06
  • first of all remove RegisterUser(); method from on create it calls directly on create without clicking register button Commented Jun 26, 2019 at 9:10
  • thank you so much for you're help. The first problem is now fixed yet i am still facing my second problem, I cannot register for some odd reason. Commented Jun 26, 2019 at 9:14

2 Answers 2

1
    Remove  RegisterUser(); from onCreate

    and try this - 

        private void doNormalLogin(String email, String password){
      FirebaseAuth mAuth = FirebaseAuth.getInstance();
            mAuth.signInWithEmailAndPassword(email, password).addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                @Override
                public void onComplete(@NonNull Task<AuthResult> task) {
                    if (task.isSuccessful()) {
                    Toast.makeText(RegisterActivity.this, "Registration successful", Toast.LENGTH_SHORT).show();
                    } else {
                    Toast.makeText(RegisterActivity.this, "Registration failed", Toast.LENGTH_SHORT).show();
                    }
                }
            });
        }

Make sure you added this in your app gradle dependencies  and the bottom plugin also - 

  dependencies {
      implementation fileTree(include: ['*.jar'], dir: 'libs')
      implementation 'com.google.firebase:firebase-auth:11.2.2'
      implementation 'com.google.firebase:firebase-database:11.2.2'
  }
  apply plugin: 'com.google.gms.google-services'

  And in your Project gradle -

  dependencies {
       classpath 'com.android.tools.build:gradle:3.2.1'
       classpath 'com.google.gms:google-services:4.2.0'
   }
Sign up to request clarification or add additional context in comments.

10 Comments

Did you check your settings in firebase?
Something is getting missed. Need to check the flow agin.
Print aa message as Mehedi Hasan said
Getting the message will give a better understanding of the issue.
Can you check with another email?
|
1

You are calling RegisterUser() method from onCreate(), that's why this method is called before inserting any info.

So remove RegisterUser() call from onCreate() only call it when user clicks on SignUp button.

2 Comments

thank you for you're help. I have done that but i still have an existing problem. It wont register for some odd reason.
Insert a log like this Log.e("SignUpError :", task.getException().getMessage()); in onComplete() method and check the message.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.