简体   繁体   中英

Registration on firebase fail java android studio

I am new at this and trying to understand why my codes don't work.

There are no error but the following do not work

  • No toast works apart the "Authentication failed!"
  • when I sign up correctly, based all my restrictions, it crushes with error line 90 of the class
  • once i register no info shows on firebase website, under authentication, user tab

the codes below, please help

package com.mz4466.photowar;
import android.annotation.SuppressLint;
import android.content.Intent;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.text.method.PasswordTransformationMethod;
import android.util.Log;
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.Task;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;

public class RegisterActivity extends AppCompatActivity {
    DatabaseHelper db;
    EditText mTextUsername;
    EditText mTextPassword;
    EditText mTextCnfPassword;
    Button mButtonRegister;
    TextView mTextViewLogin;
    private FirebaseAuth mAuth;

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


        db = new DatabaseHelper(this);
        mAuth = FirebaseAuth.getInstance();
        mTextUsername = (EditText)findViewById(R.id.edittext_email);
        mTextPassword = (EditText)findViewById(R.id.edittext_password);
        mTextPassword.setTransformationMethod(new AsteriskPasswordTransformationMethod());
        mTextCnfPassword = (EditText)findViewById(R.id.edittext_cnf_password);
        mTextCnfPassword.setTransformationMethod(new AsteriskPasswordTransformationMethod());
        mButtonRegister = (Button)findViewById(R.id.button_register);
        mTextViewLogin = (TextView)findViewById(R.id.textview_login);


        mTextViewLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent LoginIntent = new Intent(RegisterActivity.this,LoginActivity.class);
                startActivity(LoginIntent);
            }
        });


        mButtonRegister.setOnClickListener(new View.OnClickListener() {
            @SuppressLint("ShowToast")
            @Override
            public void onClick(View view) {
                String email = mTextUsername.getText().toString().trim();
                String pwd = mTextPassword.getText().toString().trim();
                String cnf_pwd = mTextCnfPassword.getText().toString().trim();

                if (TextUtils.isEmpty(email)){
                    Toast.makeText(RegisterActivity.this, "Please enter email", Toast.LENGTH_SHORT);
                    return;
                }
                if (TextUtils.isEmpty(pwd)) {
                    Toast.makeText(RegisterActivity.this, "Please enter password", Toast.LENGTH_SHORT);
                    return;
                }
                if (TextUtils.isEmpty(cnf_pwd)) {
                    Toast.makeText(RegisterActivity.this, "Please enter confirm password", Toast.LENGTH_SHORT);
                    return;
                }
                if (pwd.length()<6){
                    Toast.makeText(RegisterActivity.this, "Minimum 6 digit", Toast.LENGTH_SHORT);
                    return;
                }

                if(pwd.equals(cnf_pwd)){
                    mAuth.createUserWithEmailAndPassword(email, pwd)
                            .addOnCompleteListener(RegisterActivity.this, new OnCompleteListener<AuthResult>() {
                                @Override
                                public void onComplete(@NonNull Task<AuthResult> task) {
                                    if (!task.isSuccessful()) {
                                        startActivity(new Intent(getApplicationContext(),HomeActivity.class));
                                        Toast.makeText(RegisterActivity.this,"You have registered!",Toast.LENGTH_SHORT).show();
                                    } else {
                                        Toast.makeText(RegisterActivity.this,"Authentication Failed!",Toast.LENGTH_SHORT).show();
                                    }
                                }
                            });
                }
                else{
                    Toast.makeText(RegisterActivity.this,"Password is not matching",Toast.LENGTH_SHORT).show();
                }

            }
        });


    }

    public class AsteriskPasswordTransformationMethod extends PasswordTransformationMethod {
        @Override
        public CharSequence getTransformation(CharSequence source, View view) {
            return new PasswordCharSequence(source);
        }

        private class PasswordCharSequence implements CharSequence {
            private CharSequence mSource;
            public PasswordCharSequence(CharSequence source) {
                mSource = source; // Store char sequence
            }
            public char charAt(int index) {
                return '*'; // This is the important part
            }
            public int length() {
                return mSource.length(); // Return default
            }
            public CharSequence subSequence(int start, int end) {
                return mSource.subSequence(start, end); // Return default
            }
        }
    }
}

the activity is

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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="@color/primary_light"
    android:orientation="vertical"
    android:gravity="center_horizontal"
    tools:context="com.mz4466.photowar.RegisterActivity">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="250dp"
        android:layout_marginTop="30sp"
        app:srcCompat="@drawable/logo"/>

    <EditText
        android:id="@+id/edittext_email"
        android:layout_width="206dp"
        android:layout_height="40dp"
        android:layout_marginTop="10dp"
        android:paddingLeft="2dp"
        android:background="#ffffff"
        android:drawableLeft="@drawable/username"
        android:hint="@string/email"/>

    <EditText
        android:id="@+id/edittext_password"
        android:layout_width="206dp"
        android:layout_height="40dp"
        android:layout_marginTop="20dp"
        android:background="#ffffff"
        android:drawableLeft="@drawable/password"
        android:hint="@string/password"/>

    <EditText
        android:id="@+id/edittext_cnf_password"
        android:layout_width="206dp"
        android:layout_height="40dp"
        android:layout_marginTop="20dp"
        android:background="#ffffff"
        android:drawableLeft="@drawable/password"
        android:hint="@string/confirm_password"/>
    <Button
        android:id="@+id/button_register"
        android:layout_width="206dp"
        android:layout_height="40dp"
        android:layout_marginTop="20dp"
        android:background="@color/accent"
        android:textColor="@color/primary_text"
        android:text="@string/register"/>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_marginTop="10dp">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="35dp"
            android:textColor="@color/primary_text"
            android:text="@string/already_registered"/>

        <TextView
            android:id="@+id/textview_login"
            android:layout_width="wrap_content"
            android:layout_height="35dp"
            android:paddingLeft="10dp"
            android:textStyle="bold"
            android:textColor="@color/primary_text"
            android:textSize="16sp"
            android:text="@string/login"/>

    </LinearLayout>

</LinearLayout>

Try this:

mAuth.createUserWithEmailAndPassword(email, pwd)
                            .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                                @Override
                                public void onComplete(@NonNull Task<AuthResult> task) {
                                    if (!task.isSuccessful()) {
                                        Toast.makeText(RegisterActivity.this,"Authentication Failed!",Toast.LENGTH_SHORT).show();
                                    } else {
                                        Toast.makeText(RegisterActivity.this,"You have registered!",Toast.LENGTH_SHORT).show();
                                        Intent intent = new Intent(RegisterActivity.this,HomeActivity.class);
                                        startActivity(intent);
                                    }
                                }
                            });

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