gpt4 book ai didi

android - NullPointerException FirebaseAuth signInWithEmailAndPassword

转载 作者:太空宇宙 更新时间:2023-11-03 12:46:51 29 4
gpt4 key购买 nike

我将我的 Firebase signInWithEmailAndPassword() 方法从我的登录 Activity 中移到了一个单独的网络包/类中。我已使用 createUserWithEmailAndPassword() 方法完成此操作,它按预期工作。但是,signInWithEmailAndPassword() 方法会抛出一个

java.lang.NullPointerException: Attempt to invoke virtual method 'com.google.android.gms.tasks.Task com.google.firebase.auth.FirebaseAuth.signInWithEmailAndPassword(java.lang.String, java.lang.String)' on a null object reference

在我的 FirebaseBacked.java 网络类的第 116 行抛出,该网络类由第 102 行的 LoginActivity.java onClick() 启动。

我不确定如何从这里继续。非常感谢任何有关代码示例的帮助。

登录 Activity :

public class LoginActivity extends FirebaseBackend {

// UI references.
private ShowPassword inputPassword;
private AutoCompleteTextView inputEmail;
private FirebaseAuth auth;
private ProgressBar progressBar;
private Context context;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.context = this;

// Get Firebase auth instance
auth = FirebaseAuth.getInstance();

if (auth.getCurrentUser() != null) {
startActivity(new Intent(LoginActivity.this, MainActivity.class));
finish();
}

// Inflate interfaces
setContentView(R.layout.activity_login);

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

inputEmail = (AutoCompleteTextView) findViewById(R.id.email);
inputPassword = (ShowPassword) findViewById(R.id.password);
progressBar = (ProgressBar) findViewById(R.id.progressBar);
Button btnSignup = (Button) findViewById(R.id.btn_signup);
Button btnLogin = (Button) findViewById(R.id.btn_login);
Button btnReset = (Button) findViewById(R.id.btn_reset_password);

btnSignup.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(LoginActivity.this, RegisterActivity.class));
}
});

btnReset.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(LoginActivity.this, ResetPasswordActivity.class));
}
});

btnLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

String email = inputEmail.getText().toString();
final String password = inputPassword.getText().toString();
View focusView;

if (inputEmail.getText().length() == 0 || !android.util.Patterns.EMAIL_ADDRESS.matcher(email).matches()) {
inputEmail.setError(getString(R.string.error_invalid_email));
focusView = inputEmail;
focusView.requestFocus();
return;
}

if (!PasswordValidateUtility.isValidPassword(inputPassword.getText().toString().trim())) {
inputPassword.setError(getString(R.string.error_invalid_password));
focusView = inputPassword;
focusView.requestFocus();
return;
}

progressBar.setVisibility(View.VISIBLE);
signInWithMyApp();
}
});
}

}

Firebase后端

public class FirebaseBackend extends AppCompatActivity {

protected AutoCompleteTextView inputEmail;
protected ShowPassword inputPassword;
protected ProgressBar progressBar;
protected FirebaseAuth auth;
protected Context context;

public void createNewUser() {

String email = inputEmail.getText().toString().trim();
String password = inputPassword.getText().toString().trim();

progressBar.setVisibility(View.VISIBLE);

//create user
auth.createUserWithEmailAndPassword(email, password)
.addOnCompleteListener(FirebaseBackend.this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
progressBar.setVisibility(View.GONE);
/**
* If creating the user in the FireBase backend fails, display a message
* to the user with a dialog. If sign in succeeds, the auth state listener
* will be notified and the logic to handle the signed in user can be
* handled by the listener.
*/
if (!task.isSuccessful()) {
/**
* FireBase failed creating the user on the backend. Most likely because
* the email address is already in use or there was a connection issue.
* We will show the user a `Failed Dialog` and ask them to try again.
*/
// Signup Failed Dialog
DialogChooser.failCreateUser(context);
} else {
/**
* The user was successfully created on the backend, so now ask FireBase
* to send an authentication email to the new user.
*/
sendVerificationEmail();
}

}
});
}

public void sendVerificationEmail() {

FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();

user.sendEmailVerification()
.addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if (!task.isSuccessful()) {
/**
* FireBase failed to send a verification email so we will show a
* `Failed Send Verification Dialog` to the user, sign out the user
* and override the pent.
*/
overridePendingTransition(0, 0);
FirebaseAuth.getInstance().signOut();
startActivity(getIntent());
DialogChooser.failSendVerification(context);
} else {
/**
* FireBase successfully sent a verification email so now we show a
* `Thank-you Dialog` to the user that allows them to check their email
* for the verification.
*/
DialogChooser.agreementAccepted(context);
FirebaseAuth.getInstance().signOut();
}
}
});
}

public void signInWithMyApp() {

String email = inputEmail.getText().toString();
final String password = inputPassword.getText().toString();

//Get Firebase auth instance - Just added
auth = FirebaseAuth.getInstance();

auth.signInWithEmailAndPassword(email, password)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
/**
* 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
*/
progressBar.setVisibility(View.GONE);

if (!task.isSuccessful()) {
/**
* If the user attempts to sign in with an account that is
* already in use, we'll show then a `FailSignIn Dialog` letting
* them know and give them the option to try with another email
* ID or signup with a new email address.
*/
DialogChooser.failSignIn(context);
} else {
// Check if the user has been verified.
checkIfEmailVerified();
}
}

});

}

public void checkIfEmailVerified() {

FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();

if (user.isEmailVerified()) {
/**
* If the user is verified, finish this activity and send them to the
* MainActivity and show a success toast message.
*/
Toast.makeText(this, getString(R.string.toast_login_success), Toast.LENGTH_SHORT).show();
Intent intent = new Intent(this, MainActivity.class);
this.startActivity(intent);
finish();
} else {
/**
* If the email is not verified, prompt a message to the user using
* the `failEmailVerified` dialog and make sure the user is still
* signed out.
*/
progressBar.setVisibility(View.GONE);
DialogChooser.failEmailVerified(context);
FirebaseAuth.getInstance().signOut();
}
}
}

最佳答案

Frank van Puffelen 的最后评论是一个有效的答案。您尚未在 FirebaseBackend Activity 中实例化 auth 对象。

auth = FirebaseAuth.getInstance();

关于android - NullPointerException FirebaseAuth signInWithEmailAndPassword,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41966213/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com