简体   繁体   中英

I'm having an error creating an registration using Firebase My app is actually running but when I pressed on register button It keep on showing fail

When I pressed Register Button It keep on showing error like failed to registered

This is my Javacode

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

import android.content.Intent;
import android.os.Bundle;
import android.os.PatternMatcher;
import android.util.Patterns;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.TextView;
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.database.FirebaseDatabase;

public class RegisterUser extends AppCompatActivity implements View.OnClickListener{
    private TextView banner, registerUser,alreadyhaveaacount;
    private EditText editTextFullname, editTextAge,editTextEmail,editTextPassword;
    private ProgressBar progressBar;
    private FirebaseAuth mAuth;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register_user);
        mAuth= FirebaseAuth.getInstance();
        banner =(TextView) findViewById(R.id.banner);
        banner.setOnClickListener(this);
        alreadyhaveaacount=findViewById(R.id.alreadyhave);
        alreadyhaveaacount.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(RegisterUser.this, MainActivity.class);
                startActivity(intent);
            }
        });

        registerUser= (Button) findViewById(R.id.registerUser);
        registerUser.setOnClickListener(this);
        editTextFullname =(EditText)findViewById(R.id.fullname);
        editTextAge =(EditText)findViewById(R.id.age);
        editTextEmail =(EditText)findViewById(R.id.email);
        editTextPassword =(EditText)findViewById(R.id.password);
        progressBar = (ProgressBar) findViewById(R.id.progressBar);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.banner:
                startActivity(new Intent(this, MainActivity.class));
                break;
            case R.id.registerUser:
                registerUser();
                break;
        }
    }

    private void registerUser() {
        String email = editTextEmail.getText().toString().trim();
        String password = editTextPassword.getText().toString().trim();

        String age = editTextAge.getText().toString().trim();
        String fullname = editTextFullname.getText().toString().trim();

        if(fullname.isEmpty()){
            editTextFullname.setError("Fullname is Required!");
            editTextFullname.requestFocus();
            return;
        }
        if(fullname.length()>50){
            editTextFullname.setError("Input cannot be lower than 50!");
            editTextFullname.requestFocus();
            return;
        }
        if(password.isEmpty()){
            editTextPassword.setError("Password is Required!");
            editTextPassword.requestFocus();
            return;
        }
        if(password.length()<6){
            editTextPassword.setError("Password is too short!");
            editTextPassword.requestFocus();
            return;
        }
        if(age.isEmpty()){
            editTextAge.setError("Age is Required!");
            editTextAge.requestFocus();
            return;
        }
        if(age.length()>3){
            editTextAge.setError("Age cannot be higher than 3 digits!");
            editTextAge.requestFocus();
            return;
        }
        if(email.isEmpty()){
            editTextAge.setError("Email is required!");
            editTextAge.requestFocus();
            return;
        }if(!Patterns.EMAIL_ADDRESS.matcher(email).matches()){
            editTextEmail.setError("Please Provide a valid email!");
            editTextEmail.requestFocus();
            return;
        }

        progressBar.setVisibility(View.VISIBLE);
        mAuth.createUserWithEmailAndPassword(email, password)
                .addOnCompleteListener(new OnCompleteListener<AuthResult>() {
                    @Override
                    public void onComplete(@NonNull Task<AuthResult> task) {
                        if(task.isSuccessful()){
                            USer user = new USer(fullname,age,email,password);
                            FirebaseDatabase.getInstance().getReference("Users")
                                    .child(FirebaseAuth.getInstance().getCurrentUser().getUid())
                                    .setValue(user).addOnCompleteListener(new OnCompleteListener<Void>() {
                                @Override
                                public void onComplete(@NonNull Task<Void> task) {
                                    if (task.isSuccessful()){
                                        Toast.makeText(RegisterUser.this, "User has been registered succesfully", Toast.LENGTH_LONG).show();
                                        progressBar.setVisibility(View.GONE);
                                    }else{
                                        Toast.makeText(RegisterUser.this, "Failed To Registered! Try Again", Toast.LENGTH_LONG).show();
                                        progressBar.setVisibility(View.GONE);
                                    }
                                }
                            });
                        }else{
                            Toast.makeText(RegisterUser.this, "Failed To Registered! Try Again", Toast.LENGTH_LONG).show();
                            progressBar.setVisibility(View.GONE);
                        }
                    }
                });

    }
}

This is my Register_User.xml

 <?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#8BC34A"
    tools:context=".RegisterUser"
    android:orientation="vertical">


<androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">


    <TextView
        android:id="@+id/banner"
        android:layout_width="271dp"
        android:layout_height="50dp"
        android:layout_marginStart="176dp"
        android:layout_marginLeft="176dp"
        android:layout_marginTop="16dp"
        android:layout_marginEnd="177dp"
        android:layout_marginRight="177dp"
        android:fontFamily="sans-serif-black"
        android:gravity="center_horizontal"
        android:text="Plantured"
        android:textAlignment="center"
        android:textSize="40dp"
        android:textColor="@color/black"
        android:textStyle="bold"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.497"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/registerUser"
        android:layout_width="380dp"
        android:layout_height="70dp"
        android:layout_marginStart="50dp"
        android:layout_marginLeft="50dp"
        android:layout_marginTop="4dp"
        android:layout_marginEnd="50dp"
        android:layout_marginRight="50dp"
        android:backgroundTint="#D322D60E"
        android:text="Register"
        android:textColor="#ffffff"
        android:textSize="26sp"
        android:textStyle="bold"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.492"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textInputLayout5"
        app:layout_constraintVertical_bias="0.0" />

    <TextView
        android:id="@+id/alreadyhave"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="15dp"
        android:layout_marginBottom="16dp"
        android:gravity="center"
        android:text="Already have an account Login Here!"
        android:textColor="@color/black"
        android:textSize="23dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/registerUser" />



    <com.google.android.material.textfield.TextInputLayout
        android:id="@+id/textInputLayout"
        style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="50dp"
        android:layout_marginLeft="50dp"
        android:layout_marginTop="1dp"
        android:layout_marginEnd="50dp"
        android:layout_marginRight="50dp"
        android:hint="Fullname"
        app:hintTextColor="@color/black"
        android:paddingTop="5dp"
        app:counterEnabled="true"
        app:counterTextColor="@color/black"
        app:counterMaxLength="50"
        app:endIconMode="clear_text"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="1.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/banner"
        app:startIconDrawable="@drawable/ic_baseline_person_24">

        <com.google.android.material.textfield.TextInputEditText
            android:digits="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ "
            android:id="@+id/fullname"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/rectangle"
            android:inputType="text"
            android:paddingStart="10dp"
            android:paddingLeft="10dp"
            android:paddingEnd="10dp"
            android:textColor="@color/black"
            android:textSize="20sp" />
    </com.google.android.material.textfield.TextInputLayout>

        <com.google.android.material.textfield.TextInputLayout
            android:id="@+id/textInputLayout2"
            style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="50dp"
            android:layout_marginLeft="50dp"
            android:layout_marginEnd="50dp"
            android:layout_marginRight="50dp"
            android:hint="Age"
            app:hintTextColor="@color/black"
            app:counterTextColor="@color/black"
            app:counterEnabled="true"
            app:counterMaxLength="3"
            app:endIconMode="clear_text"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.0"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/textInputLayout"
            app:startIconDrawable="@drawable/ic_baseline_person_24">

            <com.google.android.material.textfield.TextInputEditText
                android:id="@+id/age"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@drawable/rectangle"
                android:inputType="number"
                android:paddingStart="10dp"
                android:paddingLeft="10dp"
                android:paddingEnd="10dp"
                android:textColor="@color/black"
                android:textSize="20sp" />

        </com.google.android.material.textfield.TextInputLayout>

    <com.google.android.material.textfield.TextInputLayout
        android:id="@+id/textInputLayout3"
        style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_marginStart="50dp"
        android:layout_marginLeft="50dp"
        android:layout_marginEnd="50dp"
        android:layout_marginRight="50dp"
        android:hint="Password"
        app:counterEnabled="true"
        app:counterMaxLength="15"
        app:counterTextColor="@color/black"
        app:endIconMode="password_toggle"
        app:layout_constraintEnd_toEndOf="parent"
        app:hintTextColor="@color/black"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textInputLayout2"
        app:startIconDrawable="@drawable/ic_baseline_lock_24">

        <com.google.android.material.textfield.TextInputEditText
            android:id="@+id/password"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/rectangle"
            android:inputType="textPassword"
            android:paddingStart="10dp"
            android:paddingLeft="10dp"
            android:paddingEnd="10dp"
            android:textColor="@color/black"
            android:textSize="20sp" />

    </com.google.android.material.textfield.TextInputLayout>

    <com.google.android.material.textfield.TextInputLayout
        android:id="@+id/textInputLayout5"
        style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_marginStart="50dp"
        android:layout_marginLeft="50dp"
        android:layout_marginEnd="50dp"
        android:layout_marginRight="50dp"
        android:hint="Email"
        app:counterTextColor="@color/black"
        app:counterEnabled="true"
        app:counterMaxLength="40"
        app:hintTextColor="@color/black"
        app:endIconMode="clear_text"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textInputLayout3"
        app:startIconDrawable="@drawable/ic_baseline_email_24">

        <com.google.android.material.textfield.TextInputEditText
            android:id="@+id/email"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/rectangle"
            android:inputType="textEmailAddress"
            android:paddingStart="10dp"
            android:paddingLeft="10dp"
            android:paddingEnd="10dp"
            android:textColor="@color/black"
            android:textSize="20sp" />

    </com.google.android.material.textfield.TextInputLayout>

    <ProgressBar
        android:id="@+id/progressBar"
        style="?android:attr/progressBarStyleLarge"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:visibility="gone"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:ignore="MissingConstraints" />
</androidx.constraintlayout.widget.ConstraintLayout>
</ScrollView>

**this is the USer class **

public class USer {
    public String fullName, age, email,password;

    public USer(){

    }
    public USer(String fullName, String age,String email,String password){
        this.fullName = fullName;
        this.age=age;
        this.email=email;
        this.password=password;
    }
}

This is the build.gradle(Project:App)

   // Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
    repositories {
        google()
        mavenCentral()
    }
    dependencies {
        classpath "com.android.tools.build:gradle:4.2.1"
        classpath 'com.google.gms:google-services:4.3.10'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        google()
        mavenCentral()
        // Warning: this repository is going to shut down soon
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

This is the gradle.build(Module:app)

    plugins {
    id 'com.android.application'
    id 'com.google.gms.google-services'
}

android {
    compileSdkVersion 30
    buildToolsVersion "30.0.3"

    defaultConfig {
        applicationId "com.example.authapp"
        minSdkVersion 19
        targetSdkVersion 30
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}

dependencies {

    implementation 'androidx.appcompat:appcompat:1.3.1'
    implementation 'com.google.android.material:material:1.4.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.1'
    implementation 'com.google.firebase:firebase-auth:21.0.1'
    implementation 'com.google.firebase:firebase-database:20.0.2'
    implementation platform('com.google.firebase:firebase-bom:29.0.0')
    implementation 'com.google.mlkit:image-labeling:17.0.5'
    testImplementation 'junit:junit:4.+'
    androidTestImplementation 'androidx.test.ext:junit:1.1.3'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'


}
apply plugin: 'com.google.gms.google-services'

And this is the path from my firebase realtimedatabase

enter image description here

The a Task fails, it contains an exception with the cause of the failure. Log/display that to find out the cause of the problem:

mAuth.createUserWithEmailAndPassword(email, password)
        .addOnCompleteListener(new OnCompleteListener<AuthResult>() {
            @Override
            public void onComplete(@NonNull Task<AuthResult> task) {
                if(task.isSuccessful()){
                    ...
                }else{
                    Log.e("Auth", "Failed to register", task.getException(); // 👈
                    Toast.makeText(RegisterUser.this, "Failed To Registered! Try Again", Toast.LENGTH_LONG).show();
                    progressBar.setVisibility(View.GONE);
                }
            }
        });

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM