【问题标题】:Android Studio+FireBase Account Setup throwing ErrorAndroid Studio+FireBase 帐户设置抛出错误
【发布时间】:2021-05-26 11:22:46
【问题描述】:

我似乎无法弄清楚为什么我的程序没有通过帐户注册活动。

我的问题发生在这里的某个地方

else if (!(use_email.isEmpty() && use_pass.isEmpty())) {
                    mFirebaseAuth.createUserWithEmailAndPassword( use_email, use_pass).addOnCompleteListener(SignUpActivity.this, new OnCompleteListener<AuthResult>() {
                        @Override
                        public void onComplete(@NonNull Task<AuthResult> task) {

                            if (task.isSuccessful()) {
                                startActivity(new Intent(SignUpActivity.this, LoginFormActivity.class));
                            }
                            else {
               //My problem happens Here
                 Toast.makeText(SignUpActivity.this, "Account Setup Error", Toast.LENGTH_SHORT).show();

SignUpActivity

package com.example.vehicledoctor;

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

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
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;

import java.time.Instant;

public class SignUpActivity extends AppCompatActivity {

    public EditText name, pNumber, email, username, password;
    CheckBox Nissan, Jeep, Honda, Ford, Chevrolet, Toyota, Subaru;
    Button btn_signup;
    private FirebaseAuth mFirebaseAuth = FirebaseAuth.getInstance();

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

        FirebaseUser currentUser = mFirebaseAuth.getCurrentUser();
        if(currentUser != null)
        {
            Toast.makeText(SignUpActivity.this,"Already Signed In", Toast.LENGTH_SHORT).show();
            startActivity(new Intent(SignUpActivity.this,MainMenu.class));
        }
        //mFirebaseAuth = FirebaseAuth.getInstance();
        email = findViewById(R.id.edit_email);
        password = findViewById(R.id.edit_password);
        btn_signup = findViewById(R.id.btn_enter);
        btn_signup.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                String use_email = email.getText().toString();
                String use_pass = password.getText().toString();

                if (use_email.isEmpty()) {
                    email.setError("Please Enter Email");
                    email.requestFocus();
                }

                else if (use_pass.isEmpty()) {
                    password.setError("Please Enter Password");
                    password.requestFocus();
                }

                else if (use_email.isEmpty() && use_pass.isEmpty()) {
                    Toast.makeText(SignUpActivity.this, "Fields Are Empty", Toast.LENGTH_SHORT).show();
                }

                else if (!(use_email.isEmpty() && use_pass.isEmpty())) {
                    mFirebaseAuth.createUserWithEmailAndPassword( use_email, use_pass).addOnCompleteListener(SignUpActivity.this, new OnCompleteListener<AuthResult>() {
                        @Override
                        public void onComplete(@NonNull Task<AuthResult> task) {

                            if (task.isSuccessful()) {
                                startActivity(new Intent(SignUpActivity.this, LoginFormActivity.class));
                            }
                            else {
               //My problem happens Here
                 Toast.makeText(SignUpActivity.this, "Account Setup Error", Toast.LENGTH_SHORT).show();

                            }
                        }
                    });
                }
                else {
                    Toast.makeText(SignUpActivity.this, "Error Occurred!", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }
}

我尝试了更长的密码,并检查了我的 Pixel 2 API 30 模拟器是否已连接到互联网。我还确保在 Firebase 控制台上启用身份验证。

build.gradle(app)

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

android {
    compileSdkVersion 30
    buildToolsVersion "30.0.3"

    defaultConfig {
        applicationId "com.example.vehicledoctor"
        minSdkVersion 16
        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.2.0'
    implementation 'com.google.android.material:material:1.2.1'
    implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
    implementation 'com.google.firebase:firebase-auth:19.3.1'
    testImplementation 'junit:junit:4.+'
    androidTestImplementation 'androidx.test.ext:junit:1.1.2'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
    implementation platform('com.google.firebase:firebase-bom:26.5.0')
    implementation 'com.google.firebase:firebase-analytics'
    implementation 'com.google.firebase:firebase-analytics'
}

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:4.1.2"
        classpath 'com.google.gms:google-services:4.3.5'
        // 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
}

【问题讨论】:

    标签: java android firebase android-studio firebase-authentication


    【解决方案1】:

    可能是版本的问题。使用这个版本 implementation platform('com.google.firebase:firebase-bom:26.4.0')

    【讨论】:

    • 感谢您的回复!我做了改变,但不幸的是,结果是一样的。
    【解决方案2】:

    确保您必须在 firebase 控制台中启用电子邮件和密码作为登录方法

    【讨论】:

    • 感谢您的回复,我已在 Firebase 控制台上启用了电子邮件/密码。
    【解决方案3】:

    如果您使用的是实现平台('com.google.firebase:firebase-bom:26.5.0')

    那么您不需要定义版本: 实施 'com.google.firebase:firebase-auth:19.3.1'

    bom 自动获取最新版本 替换将是: 实施 'com.google.firebase:firebase-auth

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-09-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-06
      相关资源
      最近更新 更多