最近为我的应用程序创建了一个简单的注册页面,该页面采用了电子邮件和密码这两个值,检查它们是否有效然后将它们传递给firebase函数来创建用户。值正在正确传递,但是,我总是收到消息“注册失败”。 firebase连接处于活动状态,我允许用户使用电子邮件和密码验证进行注册。
我觉得这是我的build.gradle
文件的问题,可能有错误的依赖关系,但是,我是Firebase的新手并且无法解决这个问题。
下面是我的Register表单,build.gradle文件和错误的控制台日志。任何帮助将不胜感激。
注册表格
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
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 RegisterPage extends AppCompatActivity {
private EditText userEmail,userPassword;
private Button regButton;
private FirebaseAuth firebaseAuth;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register_page);
setUpUIViews();
firebaseAuth = FirebaseAuth.getInstance();
regButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (validate()){
//Upload Data to the database
String user_email = userEmail.getText().toString().trim();
String user_password =
userPassword.getText().toString().trim();
firebaseAuth.createUserWithEmailAndPassword(user_email,user_password).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if(task.isSuccessful()){
Toast.makeText(RegisterPage.this, "Registration Complete", Toast.LENGTH_SHORT).show();
startActivity(new Intent(RegisterPage.this,MainActivity.class));
}
else {
// FirebaseAuthException e = (FirebaseAuthException)task.getException();
// Toast.makeText(RegisterPage.this,"Failed Registation: "+e.getMessage(),Toast.LENGTH_SHORT).show();
Toast.makeText(RegisterPage.this, "Registration Failed", Toast.LENGTH_SHORT).show();
}
}
});
}
}
});
}
private void setUpUIViews(){
userEmail = (EditText)findViewById(R.id.Email_Register);
userPassword = (EditText)findViewById(R.id.Password_Register);
regButton = (Button)findViewById(R.id.Register_btn);
}
private Boolean validate(){
Boolean result = false;
String email = userEmail.getText().toString();
String password = userPassword.getText().toString();
if (email.isEmpty() && password.isEmpty()){
Toast.makeText(this,"Please enter all your details correctly", Toast.LENGTH_SHORT).show();
}
else {
result = true;
}
return result;
}
}
Build.Gradle(项目) buildscript {
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.0.1'
// 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.0'
}
}
allprojects {
repositories {
google()
jcenter()
//added maven
maven {
url "https://maven.google.com" // Google's Maven repository
}
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
Build.Gradle(app)
apply plugin: 'com.android.application'
android {
compileSdkVersion 26
defaultConfig {
applicationId "com.example.bradwaterhouse.discovernottingham"
minSdkVersion 15
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(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:26.1.0'
implementation 'com.android.support.constraint:constraint-layout:1.0.2'
implementation 'com.google.firebase:firebase-database:11.0.4'
implementation 'com.google.firebase:firebase-auth:11.0.4'
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'
//added in
compile 'com.google.firebase:firebase-auth:11.0.4'
}
apply plugin: 'com.google.gms.google-services'
控制台日志
如果我在else循环中替换“登录失败”文本,这是我得到的错误消息。
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.bradwaterhouse.discovernottingham, PID: 4473
java.lang.ClassCastException: com.google.firebase.FirebaseException cannot be cast to com.google.firebase.auth.FirebaseAuthException
at com.example.bradwaterhouse.discovernottingham.RegisterPage$1$1.onComplete(RegisterPage.java:59)
at com.google.android.gms.tasks.zzf.run(Unknown Source:23)
at android.os.Handler.handleCallback(Handler.java:789)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
Application terminated.
答案 0 :(得分:1)
与OP交谈后,错误是:
I /错误是:发生了内部错误。 [未配置访问权限。之前或已禁用的Google Identity Toolkit API尚未在项目248549959996中使用。访问console.developers.google.com/...启用它,然后重试。如果您最近启用了此API,请等待几分钟,以便将操作传播到我们的系统并重试。 ]
要解决这个问题,他必须启用google identity toolkit api。
最好添加以下内容以便能够确切地知道问题是什么:
在你的其他地方加上这个,你会得到更清楚的错误:
Toast.makeText(RegisterPage.this, "User Authentication Failed: " + task.getException().getMessage(), Toast.LENGTH_SHORT).show();
或将其添加到Log
:
Log.i("error is :", task.getException().getMessage());
答案 1 :(得分:0)
您可以避免以下异常: -
if (ex instanceof FirebaseAuthException) {
FirebaseAuthError error = FirebaseAuthError.fromException((FirebaseAuthException) ex);
// TODO
} else {
// TODO
}