使用 Firebase 通过电子邮件和密码对用户进行身份验证



我正在尝试为我的应用使用 Firebase 建立用户身份验证,运行以下代码,我得到:

错误: 找不到符号 mAuth.createUserWithEmailAndPassword(电子邮件,密码(.addOnCompleteListener...

当我为onCompleteListener创建一个抽象的内部类时,它会编译但onClick不会启动,因为当您单击注册按钮时没有任何反应,甚至没有错误消息。我定义了Toast.makeText(SignUpActivity.this, "Error !" + task.getException().getMessage(), Toast.LENGTH_SHORT).show();,没有任何反应:没有点击声音,就像我在单击ontextview切换到登录屏幕或文本视图切换回注册页面时听到的声音。不会触发任何事件。

我做错了什么,我已经阅读了我能找到的所有文档......

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.text.method.LinkMovementMethod;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import com.bluribbon.dutchclone.ui.login.LoginActivity;
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 SignUpActivity extends AppCompatActivity {
private static final String TAG = "EmailPassword";
private EditText userName, userPhone, userEmail, userPassword, userPasswordConfirm, userCity;
private Button regButton;
private TextView userLogin;
private CheckBox checkBox;
private FirebaseAuth mAuth;
private ProgressBar progressBar;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_signup);
userName = findViewById(R.id.fullName);
userCity = findViewById(R.id.city);
userPhone = findViewById(R.id.phone);
userEmail = findViewById(R.id.emailin);
userPassword = findViewById(R.id.passwordin);
userPasswordConfirm = findViewById(R.id.passwordconfirm);
checkBox = findViewById(R.id.checkBox);
regButton = findViewById(R.id.Register);
userLogin = findViewById(R.id.SignIn);
progressBar = findViewById(R.id.progressBar);
mAuth = FirebaseAuth.getInstance();
TextView login = findViewById(R.id.SignIn);
login.setMovementMethod(LinkMovementMethod.getInstance());
login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(SignUpActivity.this, LoginActivity.class);
startActivity(intent);
}
});
regButton = findViewById(R.id.Register);
regButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
regButton.setVisibility(View.GONE);
progressBar.setVisibility(View.VISIBLE);
String email = userEmail.getText().toString();
String password = userPassword.getText().toString();
String passwordConfirm = userPasswordConfirm.getText().toString();
String name = userName.getText().toString();
String city = userCity.getText().toString();
String phone = userPhone.getText().toString();
if (mAuth.getCurrentUser() !=null){
startActivity(new Intent(getApplicationContext(), MainActivity.class));
finish();
}

if (TextUtils.isEmpty(email)){
userEmail.setError("Email is Required");
return;
}
if (TextUtils.isEmpty(password)){
userPassword.setError("Password is Required");
return;
}
if (TextUtils.isEmpty(passwordConfirm)){
userPasswordConfirm.setError("Please Confirm Password");
return;
}
if (TextUtils.isEmpty(name)){
userName.setError("Name is Required");
return;
}
if (TextUtils.isEmpty(city)){
userCity.setError("City is Required");
return;
}
if (TextUtils.isEmpty(phone)){
userPhone.setError("Phone is Required");
return;
}
regButton.setVisibility(View.VISIBLE);
progressBar.setVisibility(View.VISIBLE);
mAuth.createUserWithEmailAndPassword(email, password).addOnCompleteListener(this, new onCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()){
Toast.makeText(SignUpActivity.this, "Your Dutch Profile Has been Created", Toast.LENGTH_SHORT).show();
startActivity(new Intent(getApplicationContext(), MainActivity.class));
progressBar.setVisibility(View.GONE);
}
else {
Toast.makeText(SignUpActivity.this, "Error !" + task.getException().getMessage(), Toast.LENGTH_SHORT).show();
}
}
});
}
});
}
}

添加 addonSuccessListener 而不是 onComplete

相关内容

  • 没有找到相关文章

最新更新