我使用Firebase身份验证创建了一个注册活动,但是当我运行应用程序并单击注册按钮时,没有任何反应。只有进度对话框,只是旋转,它甚至没有注册。
这是我的代码:
public class RegisterActivity extends AppCompatActivity {
private EditText mNameField;
private EditText mEmailField;
private EditText mPasswordField;
private Button mRegisterBtn;
private FirebaseAuth mAuth;
private DatabaseReference mDatabase;
private ProgressDialog mProgress;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
mAuth = FirebaseAuth.getInstance();
mDatabase = FirebaseDatabase.getInstance().getReference().child("users");
mProgress = new ProgressDialog(this);
mNameField = (EditText) findViewById(R.id.nameFied);
mEmailField = (EditText) findViewById(R.id.emailField);
mPasswordField = (EditText) findViewById(R.id.passwordField);
mRegisterBtn = (Button) findViewById(R.id.registerBtn);
mRegisterBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startRegister();
}
});
}
private void startRegister() {
final String name = mNameField.getText().toString().trim();
String email = mEmailField.getText().toString().trim();
String password = mPasswordField.getText().toString().trim();
if(!TextUtils.isEmpty(name) && !TextUtils.isEmpty(email) && !TextUtils.isEmpty(password)){
mProgress.setMessage("Signing up...please wait");
mProgress.show();
mAuth.createUserWithEmailAndPassword(email, password).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()){
String user_id = mAuth.getCurrentUser().getUid();
DatabaseReference current_user_db = mDatabase.child(user_id);
current_user_db.child("name").setValue(name);
current_user_db.child("image").setValue("default");
mProgress.dismiss();
Intent mainIntent = new Intent(RegisterActivity.this, MainActivity.class);
mainIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(mainIntent);
}
}
});
}
if (TextUtils.isEmpty(name) && TextUtils.isEmpty(email) && TextUtils.isEmpty(password)){
Toast.makeText(RegisterActivity.this, "do not leave any field empty", Toast.LENGTH_LONG).show();
}
}
}
我不知道自己做错了什么。
以下是我的gradle依赖项:
compile 'com.google.firebase:firebase-database:9.2.0'
compile 'com.google.firebase:firebase-auth:9.2.0'
答案 0 :(得分:0)
尝试添加OnFailureListener
以查看您获得的异常或错误。
mAuth.createUserWithEmailAndPassword(email, password).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()){
String user_id = mAuth.getCurrentUser().getUid();
DatabaseReference current_user_db = mDatabase.child(user_id);
current_user_db.child("name").setValue(name);
current_user_db.child("image").setValue("default");
mProgress.dismiss();
Intent mainIntent = new Intent(RegisterActivity.this, MainActivity.class);
mainIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(mainIntent);
}
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Log.i("Firebase Exception -> ",e.getMessage());
}});
答案 1 :(得分:0)
您正在使用
创建用户XML
但你永远不会登录。您还需要在使用
创建用户后登录createUserWithEmailAndPassword(email, password)
并将数据添加到数据库中。
我建议您也更新您的存储库和版本。