主要活动:
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
TextView email;
TextView password;
TextView sysAnswer;
String[] domenArray = {"@gmail.com", "@mail.ru", "@ya.ru", "@yandex.ru", "@yahoo.ru"};
ImageButton enter;
ImageButton registration;
private FirebaseAuth mAuth;
private FirebaseAuth.AuthStateListener mAuthListener;
AuthHandler aH = new AuthHandler();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
email = (TextView) findViewById(R.id.email);
password = (TextView) findViewById(R.id.password);
sysAnswer = (TextView) findViewById(R.id.sysAnswer);
mAuth = FirebaseAuth.getInstance();
mAuthListener = new FirebaseAuth.AuthStateListener() {
@Override
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) { // слушает действия пользователя
FirebaseUser user = firebaseAuth.getCurrentUser(); // зашел, вышел и что с этим делать
if (user != null) {
// User is signed in
} else {
// User is signed out
}
}
};
findViewById(R.id.registration).setOnClickListener(MainActivity.this);
findViewById(R.id.sign_in).setOnClickListener(MainActivity.this);
}
@Override
public void onClick(View v) {
if (v.getId() == R.id.registration) {
aH.registration(email.getText().toString(), password.getText().toString());
if (v.getId() == R.id.sign_in) {
aH.singing(email.getText().toString(), password.getText().toString());
//if (aH.singingResult){
// Toast.makeText(this, "Успешный вход!", Toast.LENGTH_SHORT).show();
//} else {
// Toast.makeText(this, "Ошибка! Включите интернет, проверьте правильность форм и попробуйте снова!", Toast.LENGTH_SHORT).show();
//}
}
}
}
}
二等。
public class AuthHandler {
private FirebaseAuth mAuth;
private boolean registrationResult = false;
private boolean singingResult = false;
public void singing(String email, String password) {
mAuth.signInWithEmailAndPassword(email, password);
}
public void registration(String email, String password){
mAuth.createUserWithEmailAndPassword(email, password).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if(task.isSuccessful()) {
registrationResult=true;
}else {
registrationResult = false;
}
}
});
}
}
我已经编译了这段代码,但是在我点击按钮注册后,我的应用程序都被粉碎了。我认为,addOnCompleteListener
存在问题,但我不知道执行器是什么意思,或者如何将此方法作为Activity对象。你能告诉我我的错吗?谢谢你的帮助。
答案 0 :(得分:0)
尝试改变唱歌和注册方法,如下所述:
public class AuthHandler {
private FirebaseAuth mAuth;
private boolean registrationResult = false;
private boolean singingResult = false;
public void singing(String email, String password) {
// mAuth.signInWithEmailAndPassword(email, password);
mAuth.getInstance().signInWithEmailAndPassword(email, password);
}
public void registration(String email, String password){
mAuth.getInstance().createUserWithEmailAndPassword(email, password).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if(task.isSuccessful()) {
registrationResult=true;
}else {
registrationResult = false;
}
}
});
}
}