Android Studio Firebase创建新用户时出错

时间:2017-12-31 08:21:05

标签: android firebase firebase-authentication

我在Android和Firebase上做了这个教程,但出于某些原因,当我尝试注册新的驱动程序时,它根本不与Firebase交互。

我已将API降级为26。

RegisterActivity.java

package com.example.naim.chatapp;

import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.design.widget.TextInputLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
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;

public class RegisterActivity extends AppCompatActivity {

    private  TextInputLayout mDisplayName;
    private  TextInputLayout mEmail;
    private TextInputLayout mPassword;
    private  Button mCreate;
    private FirebaseAuth mAuth;

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

        mAuth = FirebaseAuth.getInstance();

        mDisplayName=(TextInputLayout)findViewById(R.id.reg_DisplayName);
        mEmail=(TextInputLayout)findViewById(R.id.reg_Email);
        mPassword=(TextInputLayout)findViewById(R.id.reg_Password);

        mCreate=(Button)findViewById(R.id.reg_Create_Button);

        mCreate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
               String displayName= mDisplayName.getEditText().getText().toString();
            final String email= mEmail.getEditText().getText().toString();
            final String pass= mPassword.getEditText().getText().toString();

               register(displayName,email,pass);

            }
        });
    }


    //New User Sign Up

    private void register(String displayName,String email, String pass) {
        mAuth.createUserWithEmailAndPassword(email, pass)
                .addOnCompleteListener(RegisterActivity.this,new OnCompleteListener<AuthResult>() {
                    @Override
                    public void onComplete(@NonNull Task<AuthResult> task) {
                        if (task.isSuccessful()) {
                            Intent mainIntent = new Intent(RegisterActivity.this, MainActivity.class);
                            startActivity(mainIntent);
                            finish();

                        } else {
                            Toast.makeText(RegisterActivity.this, "Fail to login", Toast.LENGTH_SHORT).show();
                        }
                    }
                });
    }

}

这是我的build.gradle(app)

apply plugin: 'com.android.application'

android {
    compileSdkVersion 26
    defaultConfig {
        applicationId "com.example.naim.chatapp"
        minSdkVersion 21
        targetSdkVersion 26
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    implementation 'com.android.support:appcompat-v7:26.1.0'
    implementation 'com.android.support:design:26.1.0'
    implementation 'com.android.support.constraint:constraint-layout:1.0.2'
    implementation 'com.google.firebase:firebase-core:11.6.0'
    implementation 'com.google.firebase:firebase-database:11.6.0'
    implementation 'com.google.firebase:firebase-auth:11.6.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.1'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
}

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

build.gradle(module)

// 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.1.0-alpha06'


        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
        classpath 'com.google.gms:google-services:3.1.1'
    }
}

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

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

这是我尝试在模拟器上运行应用程序时遇到的错误;

--------- beginning of crash
E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: ke.co.wafalmelogistics.wafalme, PID: 1640
                  java.lang.NullPointerException: Attempt to invoke virtual method 'com.google.android.gms.tasks.Task com.google.android.gms.common.api.GoogleApi.zzb(com.google.android.gms.common.api.internal.zzdf)' on a null object reference
                      at com.google.android.gms.internal.zzdtp.zzb(Unknown Source)
                      at com.google.android.gms.internal.zzdtw.zza(Unknown Source)
                      at com.google.firebase.auth.FirebaseAuth.createUserWithEmailAndPassword(Unknown Source)
                      at ke.co.wafalmelogistics.wafalme.DriverLoginActivity$2.onClick(DriverLoginActivity.java:61)
                      at android.view.View.performClick(View.java:5637)
                      at android.view.View$PerformClick.run(View.java:22429)
                      at android.os.Handler.handleCallback(Handler.java:751)
                      at android.os.Handler.dispatchMessage(Handler.java:95)
                      at android.os.Looper.loop(Looper.java:154)
                      at android.app.ActivityThread.main(ActivityThread.java:6119)
                      at java.lang.reflect.Method.invoke(Native Method)
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)

我是学习者,感谢任何帮助。

1 个答案:

答案 0 :(得分:1)

如果您在Android模拟器上运行此代码,则必须选择安装了google api的模拟器。