我有一个RegisterActivity,用于在firebase中签名用户。它在Android工作室模拟器上工作得很好,但不是真正的设备。它显示了ProgessDialog并且在它被卡住之后,这意味着progess对话框只运行而没有任何反应。
package com.example.ozan.chatapp;
import android.app.ProgressDialog;
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.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
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;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import java.util.HashMap;
import java.util.Map;
public class RegisterActivity extends AppCompatActivity {
private static final String TAG = "RegisterActivity";
private TextInputLayout mUsername;
private TextInputLayout mEmail;
private TextInputLayout mPassword;
private FirebaseAuth mAuth;
private Toolbar toolbar;
private ProgressDialog mprogressDialog;
private DatabaseReference database;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
mUsername = findViewById(R.id.register_username);
mEmail = findViewById(R.id.login_email);
mPassword = findViewById(R.id.login_password);
mprogressDialog = new ProgressDialog(this);
mAuth = FirebaseAuth.getInstance();
toolbar = findViewById(R.id.login_toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setTitle("Registrieren");
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
public void onClickRegistrieren(View view){
Log.d(TAG, "onClickRegistrieren: Button gedrückt");
final String username = mUsername.getEditText().getText().toString();
String email = mEmail.getEditText().getText().toString();
String password = mPassword.getEditText().getText().toString();
if(validateData(username, email, password)) {
mprogressDialog.setTitle("Registrierung");
mprogressDialog.setMessage("Bitte warte, solange wir deinen Account erstellen :)");
mprogressDialog.setCanceledOnTouchOutside(false);
mprogressDialog.show();
mAuth.createUserWithEmailAndPassword(email, password).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
String userid = user.getUid();
database = FirebaseDatabase.getInstance().getReference().child("users").child(userid);
HashMap<String, String> map = new HashMap<>();
map.put("name", username);
map.put("status", "Status");
map.put("image", "default");
map.put("thumb_image", "default");
database.setValue(map).addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if(task.isSuccessful()) {
mprogressDialog.dismiss();
Toast.makeText(RegisterActivity.this, "User wurde registriert!", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(RegisterActivity.this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
finish();
}
}
});
} else {
mprogressDialog.hide();
Log.d(TAG, "onComplete: " + task.getException());
Toast.makeText(RegisterActivity.this, "User konnte nicht registriert werden!", Toast.LENGTH_SHORT).show();
}
}
});
}
}
private boolean validateEmail(String email){
if(email.isEmpty()){
mEmail.setError("Bitte Email eintragen");
return false;
}
mEmail.setError(null);
return true;
}
private boolean validateUsername(String username){
if(username.isEmpty()){
mUsername.setError("Bitte Username eintragen");
return false;
}
mUsername.setError(null);
return true;
}
private boolean validatePassword(String password){
if(password.isEmpty()){
mPassword.setError("Bitte Passwort eintragen");
return false;
}
mPassword.setError(null);
return true;
}
private boolean validateData(String username, String email, String password){
return validateUsername(username) & validateEmail(email) & validatePassword(password);
}
}
我的意思是我在creatUserwithEmailandPassword方法上有addCompleteListener,我可以询问该任务是否成功。它甚至不会进入错误部分,我隐藏进度条并向用户发送祝酒词。感觉就像我的真实设备只是点击了creatUserwithEmailandPassword方法。