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.
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.
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'
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.