首页 > 解决方案 > FireBase android:用户注册不起作用

问题描述

我在 android 中创建了一个登录注册应用程序并使用 Firebase 来存储数据。因此,在提供电子邮件和密码后,当我单击“注册”按钮时,它会显示“注册失败通知”。我该怎么办?

代码如下:

报名活动

package com.shankhadeep.firebasedemo;

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

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
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 Registration extends AppCompatActivity {


    EditText edt_email_reg, edt_password_reg;
    Button btn_register;
    private FirebaseAuth auth;

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

        edt_email_reg = findViewById(R.id.edt_email_reg);
        edt_password_reg = findViewById(R.id.edt_password_reg);
        btn_register = findViewById(R.id.btn_register);

        auth = FirebaseAuth.getInstance();



        btn_register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String email= edt_email_reg.getText().toString().trim();
                String password = edt_password_reg.getText().toString().trim();

                if(TextUtils.isEmpty(email) || TextUtils.isEmpty(password)){
                    Toast.makeText(Registration.this,"Empty Credentials",Toast.LENGTH_LONG).show();


                }

                else if(password.length()<6){
                    Toast.makeText(Registration.this,"Password too short! Minimum 8 characters required",Toast.LENGTH_LONG).show();

                }

                else{
                    registerUser(email,password);
                }
            }
        });
    }

    private void registerUser(String email, String password) {

        auth.createUserWithEmailAndPassword(email,password).addOnCompleteListener(Registration.this, new OnCompleteListener<AuthResult>() {
            @Override
            public void onComplete(@NonNull Task<AuthResult> task) {
                if (task.isSuccessful()){
                    Toast.makeText(Registration.this,"Registartion failed", Toast.LENGTH_LONG).show();

                    FirebaseUser user = auth.getCurrentUser();

                }
                else {
                    Toast.makeText(Registration.this,"Registartion failed", Toast.LENGTH_LONG).show();
                }
            }
        });

    }
}

Build.gradle(应用级别)

apply plugin: 'com.android.application'
apply plugin: 'com.google.gms.google-services'

android {
    compileSdkVersion 29
    buildToolsVersion "29.0.3"
    defaultConfig {
        applicationId "com.shankhadeep.firebasedemo"
        minSdkVersion 23
        targetSdkVersion 29
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'androidx.appcompat:appcompat:1.2.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.0.1'
    implementation 'com.google.firebase:firebase-database:17.0.0'
    implementation 'com.google.firebase:firebase-auth:17.0.0'
    implementation 'com.google.firebase:firebase-analytics:17.5.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:1.1.2'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
}

Build.gradle(项目级别)

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
    repositories {
        google()
        jcenter()
        
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.5.3'
        classpath 'com.google.gms:google-services:4.3.3'

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

allprojects {
    repositories {
        google()
        jcenter()
        
    }
}

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

**这是我收到的通知:**

在 Log-cat 中显示

日志猫:

任何建议对我来说都很好。

标签: javaandroidfirebasefailed-to-connect

解决方案


请在您的 Firebase 帐户中检查此功能的启用状态,您可以在其中启用通过电子邮件登录的选项。

打开这个菜单: 在此处输入图像描述


推荐阅读