我正在使用Firebase Auth(注册新用户),它在主线程上完美运行但是当我将其移至后台时,身份验证始终失败。 以下是创建新用户的方法:
public void createAccount(String email, String password) {
mAuth.createUserWithEmailAndPassword(email, password)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
Log.d(TAG, "createUserWithEmail:onComplete:" + task.isSuccessful());
// If sign in fails, display a message to the user. If sign in succeeds
// the auth state listener will be notified and logic to handle the
// signed in user can be handled in the listener.
if (!task.isSuccessful()) {
Toast.makeText(registerActivity.this, "bad",
Toast.LENGTH_SHORT).show();
sr = false;
} else {
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
if (user != null) {
EditText etname = (EditText) findViewById(R.id.etName);
EditText etlname = (EditText) findViewById(R.id.etLName);
String namez = etname.getText().toString();
String lnamez = etlname.getText().toString();
DatabaseReference mDatabase;
mDatabase = FirebaseDatabase.getInstance().getReference();
Toast.makeText(registerActivity.this, "good", Toast.LENGTH_SHORT).show();
String UID = user.getUid();
mDatabase.child("users").child(UID + "_lname").setValue(lnamez);
mDatabase.child("users").child(UID + "_name").setValue(namez);
sr = true;
}
}
// ...
}
});
}
这里是AsyncTask:
private class createInBack extends AsyncTask<Void, Void, Void> {
//Executes the 'createAccount' method in background .
EditText etemail = (EditText) findViewById(R.id.etEmail);
EditText etpassword = (EditText) findViewById(R.id.etPassword);
String email = etemail.getText().toString();
String password = etpassword.getText().toString();
@Override
protected void onPreExecute() {
super.onPreExecute();
spinner.setVisibility(View.VISIBLE);
}
@Override
protected Void doInBackground(Void... voids) {
createAccount(email, password);
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
if(sr){
Toast.makeText(registerActivity.this, "we good here", Toast.LENGTH_SHORT).show();
startActivity(new Intent(registerActivity.this, MainActivity.class));
}
spinner.setVisibility(View.GONE);
}
}
那么问题是什么?
答案 0 :(得分:0)
解决问题, 我在XML布局中发现我忘记从按钮中删除“android:onClick”。
答案 1 :(得分:0)
Linxy是正确的,没有必要为您的目标创建Asynctask。 Firebase为您解决了这个问题,请看一个简单的示例。
private void loginAccount(String email, String password) {
final ProgressDialog progressDialog = new ProgressDialog(Login.this,
R.style.AppTheme_Dark_Dialog);
progressDialog.setIndeterminate(false);
progressDialog.setMessage("Autenticando...");
progressDialog.show();
mAuth.signInWithEmailAndPassword(email,password).addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
login.setEnabled(true);//button-ignore
login.setAlpha(1f);
if (task.isSuccessful()){
progressDialog.dismiss();
Intent intent = new Intent(getApplicationContext(),MainActivity.class);
startActivity(intent);
finish();
//Toast.makeText(Login.this,"Sesion iniciada",Toast.LENGTH_SHORT).show();
}else{
progressDialog.dismiss();
Toast.makeText(Login.this,"Email or password invalidate",Toast.LENGTH_SHORT).show();
Log.i("sesion","MaldatosH2");
}
}
});
}
创建用户是相同的xd