Firebase身份验证createUserWithEmailAndPassword在Android中无效

时间:2017-11-20 10:35:27

标签: android firebase firebase-authentication

我正在尝试在我的应用程序中实现身份验证页面。 我在我的firebase数据库中启用了电子邮件身份验证,编码如下

public class AdminLoginActivity extends AppCompatActivity {

    private Button loginButton;
    private EditText inputEmail;
    private EditText inputPassword;
    private TextView loginText;

    //private ProgressDialog progressDialog;

    //Firebase Authentication Object

    private FirebaseAuth firebaseAuth;

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

        //progressDialog = new ProgressDialog(AdminLoginActivity.this);

        firebaseAuth = FirebaseAuth.getInstance();

        loginButton = (Button) findViewById(R.id.login_button);
        inputEmail = (EditText) findViewById(R.id.enter_email);
        inputPassword = (EditText) findViewById(R.id.enter_password);

        loginText = (TextView) findViewById(R.id.newaccount_text);

        loginButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                accountLogin();

            }
        });

        loginText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {


            }
        });
    }

    private void accountLogin() {
        String email = inputEmail.getText().toString().trim();
        String password = inputPassword.getText().toString().trim();


        // valid input
        //progressDialog.setTitle("Loading...");
        //progressDialog.setMessage("Logging in...");
        //progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        //progressDialog.show();

        // Creates a user with a given email and password in the database
                (firebaseAuth.createUserWithEmailAndPassword(email, password))
            .addOnCompleteListener(AdminLoginActivity.this, new OnCompleteListener<AuthResult>() {
        @Override
        public void onComplete(@NonNull Task<AuthResult> task) {
            if(task.isSuccessful()) {
                //successfully signed in
                //start new activity
                Toast.makeText(AdminLoginActivity.this, "Success", Toast.LENGTH_SHORT).show();
                //progressDialog.hide();
            } else {
                Toast.makeText(AdminLoginActivity.this, "Fail", Toast.LENGTH_SHORT).show();
            }
        }
    });
    }
}

当我单击提交按钮并尝试调试它时,它无法到达createUserWithEmailAndPassword功能。它在此之前就停止了。当我检查我的Firebase数据库时,没有添加任何内容。

我犯了什么错误?

编辑:

11-20 04:58:56.884 12640-12678/ykim164cs242.tournamentor E/FA: Discarding data. Failed to send app launch
11-20 04:58:56.887 12640-12678/ykim164cs242.tournamentor E/FA: Failed to get app instance id
11-20 04:58:56.889 12640-12678/ykim164cs242.tournamentor E/FA: Failed to send current screen to service
11-20 04:58:56.891 12640-12678/ykim164cs242.tournamentor E/FA: Discarding data. Failed to send event to service

EDIT2

的build.gradle

apply plugin: 'com.android.application'

android {
    compileSdkVersion 26
    buildToolsVersion "26.0.2"
    defaultConfig {
        applicationId "-----------"
        minSdkVersion 15
        targetSdkVersion 26
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        vectorDrawables.useSupportLibrary = true
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:design:26.+'
    compile 'com.android.support:appcompat-v7:26.+'
    compile 'com.android.support.constraint:constraint-layout:1.0.2'
    compile 'com.android.support:support-vector-drawable:26.+'
    compile 'com.google.firebase:firebase-core:11.0.4'
    compile 'com.google.firebase:firebase-database:11.0.4'
    compile 'com.google.firebase:firebase-auth:11.0.4'
    compile 'com.android.support:support-v4:26.+'
    testCompile 'junit:junit:4.12'
}


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

`顶级gradle&#39;

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

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.3.3'
        classpath 'com.google.gms:google-services:3.1.0'
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        jcenter()
    }
}

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

activity_admin_login.xml

<?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:orientation="vertical"
    tools:context="ykim164cs242.tournamentor.Activity.Admin.AdminLoginActivity">

    <EditText
        android:hint="Enter your email"
        android:id="@+id/enter_email"
        android:layout_margin="15dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <EditText
        android:id="@+id/enter_password"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="15dp"
        android:hint="Enter your password" />

    <Button
        android:id="@+id/login_button"
        android:text="Manage Tournament"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="15dp" />

    <TextView
        android:id="@+id/newaccount_text"
        android:text="Want to set up a new tournament? Click here"
        android:textAlignment="center"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>

UI

enter image description here

2 个答案:

答案 0 :(得分:0)

在firebase中尝试使用此代码进行注册页面,它将为您提供帮助

  private void registerUser(){

         username = editTextUsername.getText().toString();
        String password = editTextPassword.getText().toString();
        String repeatPassword = editTextRepeatPassword.getText().toString();

        //checking if email and passwords are empty
            if (validate(username, password, repeatPassword)) {
                Toast.makeText(this, "You Passed", Toast.LENGTH_SHORT).show();
            }else {
                Toast.makeText(this, "Invalid email or not match password", Toast.LENGTH_SHORT).show();
            }
        //if the email and password are not empty
        //displaying a progress dialog
        progressDialog.setMessage("Registering Please Wait...");
        progressDialog.show();

        //creating a new user
        firebaseAuth.createUserWithEmailAndPassword(username, password)
                .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                    @Override
                    public void onComplete(@NonNull Task<AuthResult> task) {
                        //checking if success
                        if(task.isSuccessful()){
                            //display some message here
                            Toast.makeText(RegistrationActivity.this,"Successfully registered",Toast.LENGTH_LONG).show();
                        }else{
                            //display some message here
                            Toast.makeText(RegistrationActivity.this,"Registration Error",Toast.LENGTH_LONG).show();
                        }
                        progressDialog.dismiss();
                    }
                });
    }
    private boolean validate(String emailStr, String password, String repeatPassword) {
        Matcher matcher = VALID_EMAIL_ADDRESS_REGEX .matcher(emailStr);
        return password.length() > 0 && repeatPassword.equals(password) && matcher.find();
    }

答案 1 :(得分:0)

很明显,您正在使用模拟器来测试您的应用程序。在这种情况下,您需要更新模拟器才能安装Play Services 11。