我将Firebase signInWithEmailAndPassword()
方法从我的登录活动中移出到单独的网络包/类中。我使用createUserWithEmailAndPassword()
方法完成了这项工作,它按预期工作。但是,signInWithEmailAndPassword()
方法会抛出
java.lang.NullPointerException:尝试调用虚方法 “com.google.android.gms.tasks.Task com.google.firebase.auth.FirebaseAuth.signInWithEmailAndPassword(java.lang.String中, “空对象引用
上的java.lang.String)”
在我的FirebaseBacked.java
网络课程的第116行投掷,该网络课程由LoginActivity.java
onClick()
在第102行发起。
我不确定如何从这里继续。任何有关代码示例的帮助都将不胜感激。
LoginActivity:
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();
}
});
}
}
FirebaseBackend
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();
}
}
}
答案 0 :(得分:5)
Frank van Puffelen的最后评论是一个有效的答案。您尚未在auth
活动中实例化FirebaseBackend
对象。
auth = FirebaseAuth.getInstance();
答案 1 :(得分:0)
在onCreate()方法上使用此代码
auth = FirebaseAuth.getInstance();
那么您将不会获得空指针异常