gpt4 book ai didi

android - 尝试调用虚拟方法 'null object reference'

转载 作者:行者123 更新时间:2023-11-29 23:43:18 26 4
gpt4 key购买 nike

每当我尝试使用 firebase auth 注册用户时,我的应用程序就会崩溃。我的代码如下:

public class MainActivity extends AppCompatActivity {


private EditText mEmailField;
private EditText mPasswordField;

private Button mLoginBtn;

private FirebaseAuth mAuth;

private FirebaseAuth.AuthStateListener mAuthListener;


protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

mAuth = FirebaseAuth.getInstance();

mEmailField = (EditText)findViewById(R.id.emailField);
mPasswordField = (EditText)findViewById(R.id.passwordField);

mLoginBtn = (Button)findViewById(R.id.buttonLogin);

mAuthListener = new FirebaseAuth.AuthStateListener() {
@Override
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {

if(firebaseAuth.getCurrentUser() != null) {

startActivity(new Intent(MainActivity.this, Home.class));

}

}
};

mLoginBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {

startSignIn();

}
});

}

@Override
protected void onStart() {
super.onStart();

mAuth.addAuthStateListener(mAuthListener);

}

private void startSignIn() {

String email = mEmailField.getText().toString();
String password = mPasswordField.getText().toString();

if(TextUtils.isEmpty(email) || TextUtils.isEmpty(password)) {

Toast.makeText(MainActivity.this, "Fields are empty", Toast.LENGTH_LONG).show();

} else {

mAuth.signInWithEmailAndPassword(email, password).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {

if (!task.isSuccessful()) {

Toast.makeText(MainActivity.this, "Sign In Failed!!", Toast.LENGTH_LONG).show();
}

}
});
}


}
}

这是我的日志:

08-03 05:50:41.766 17746-17746/com.finalyearproject.vimelraj.qfoody E/AndroidRuntime: FATAL EXCEPTION: main Process: com.finalyearproject.vimelraj.qfoody, PID: 17746 java.lang.NullPointerException: Attempt to invoke virtual method 'com.google.android.gms.tasks.Task com.google.android.gms.common.api.GoogleApi.zzb(com.google.android.gms.common.api.internal.zzdd)' on a null object reference at com.google.android.gms.internal.zzdvv.zzb(Unknown Source) at com.google.android.gms.internal.zzdwc.zzb(Unknown Source) at com.google.firebase.auth.FirebaseAuth.signInWithEmailAndPassword(Unknown Source) at com.finalyearproject.vimelraj.qfoody.MainActivity.startSignIn(MainActivity.java:91) at com.finalyearproject.vimelraj.qfoody.MainActivity.access$000(MainActivity.java:23) at com.finalyearproject.vimelraj.qfoody.MainActivity$2.onClick(MainActivity.java:64) at android.view.View.performClick(View.java:5637) at android.view.View$PerformClick.run(View.java:22429) at android.os.Handler.handleCallback(Handler.java:751) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6119) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)

最佳答案

onComplete 方法中使用下面的代码。

new android.os.Handler().postDelayed(new Runnable() {
@Override
public void run() {
Toast.makeText(MainActivity.this, "Sign In Failed!!", Toast.LENGTH_LONG).show();
}
}, 100);

更新

private void startSignIn() {

String email = mEmailField.getText().toString();
String password = mPasswordField.getText().toString();

if( email == null || password == null || TextUtils.isEmpty(email) || TextUtils.isEmpty(password)) {

Toast.makeText(MainActivity.this, "Fields are empty", Toast.LENGTH_LONG).show();

} else {

mAuth.signInWithEmailAndPassword(email, password).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {

if (!task.isSuccessful()) {

new android.os.Handler().postDelayed(new Runnable() {
@Override
public void run() {
Toast.makeText(MainActivity.this, "Sign In Failed!!", Toast.LENGTH_LONG).show();
}
}, 100);
}

}
});
}


}

关于android - 尝试调用虚拟方法 'null object reference',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51666272/

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