当Activity
以Firebase
开头的电子邮件和密码身份验证应用程序崩溃时
package company.pawelzielinski.drdtr;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
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;
public class register_with_firebase extends AppCompatActivity {
EditText email,password;
Button registerButton;
FirebaseAuth firebaseAuth;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register_with_firebase);
email = (EditText) findViewById(R.id.email_firebase_register);
password = (EditText) findViewById(R.id.password_firebase_register);
registerButton = (Button) findViewById(R.id.register_button);
firebaseAuth = FirebaseAuth.getInstance();
registerButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String email_string = email.getText().toString();
String password_string = password.getText().toString();
if(TextUtils.isEmpty(email_string)){
Toast.makeText(getApplicationContext(),"Please fill in the required fields", Toast.LENGTH_SHORT).show();
return;
}
if(TextUtils.isEmpty(password_string)){
Toast.makeText(getApplicationContext(),"Please fill in the required fields",Toast.LENGTH_SHORT).show();
}
if(password_string.length()<7){
Toast.makeText(getApplicationContext(),"Password must be at least 6 characters",Toast.LENGTH_SHORT).show();
}
firebaseAuth.createUserWithEmailAndPassword(email_string,password_string)
.addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if(task.isSuccessful()){
startActivity(new Intent(getApplicationContext(),register_window.class));
finish();
}
else{
Toast.makeText(getApplicationContext(),"E-mail or password is wrong",Toast.LENGTH_SHORT).show();
}
}
});
}
});
if(firebaseAuth.getCurrentUser()!=null){
startActivity(new Intent(getApplicationContext(),main_menu_in_game.class));
}
}
}
最佳答案
buildscript {
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.0.1'
classpath 'com.google.gms:google-services:4.1.0'
// 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
}
如下添加依赖项
implementation 'com.google.firebase:firebase-auth:16.0.4'
implementation 'com.google.firebase:firebase-database:16.0.3'
implementation 'com.google.firebase:firebase-firestore:17.1.1'