我正在学习如何使用Firebase身份验证并将用户数据放入Firebase。当我按下故意将我从CreateAccountActivity带到第二个设置活动的编码按钮时,应用程序崩溃。我的代码有什么问题?
当我注释掉AccountSetupActivity中的所有代码时,活动启动正常。但是,当我将代码放回AccountSetupActivity时,它每次启动都会崩溃。
CreateAccountActivity @Override 受保护的void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.createaccount_activity);
mAuth = FirebaseAuth.getInstance();
userId = (EditText) findViewById(...);
userPassword = (EditText) findViewById(...);
confirmPassword = (EditText) findViewById(....);
createAccount = (Button) findViewById(...);
createAccount.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
createAccount();
}
});
}
private void createAccount() {
String uID = userId.getText().toString();
String uPW = userPassword.getText().toString();
String confirmPW = confirmPassword.getText().toString();
if(TextUtils.isEmpty(uID) || TextUtils.isEmpty(uPW) || TextUtils.isEmpty(confirmPW)){
return;
}
if(uPW.length()<6){
return;
}
if(!uPW.equals(confirmPW))
return;
}
mAuth.createUserWithEmailAndPassword(uID, uPW).addOnCompleteListener(CreateAccountActivity.this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()){
sendUserInitialSetupActivity();
}else{
Toast.makeText(getApplicationContext(),"E-mail or password is wrong",Toast.LENGTH_SHORT).show();
}
}
});
}
private void sendUserInitialSetupActivity() {
Intent intent = new Intent(CreateAccountActivity.this, InitialAccountSetupActivity.class);
startActivity(intent);
finish();
}
SetupAccountActivity @Override 受保护的void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.activity_initial_account_setup);
mAuth = FirebaseAuth.getInstance();
currentUserID = mAuth.getCurrentUser().getUid();
userRef = FirebaseDatabase.getInstance().getReference().child("Users").child(currentUserID);
setImage = (ImageView) findViewById(...);
setDisplayName = (EditText) findViewById(...);
setBirthDate = (EditText) findViewById(...);
confirmButton = (Button) findViewById(...);
confirmButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
confirmAccount();
}
});
}
private void confirmAccount() {
String displayName = setDisplayName.getText().toString();
String birthDate = setBirthDate.getText().toString();
if(displayName.isEmpty() || birthDate.isEmpty()){
return;
}
HashMap userMap = new HashMap();
userMap.put("displayName", displayName);
userMap.put("birthDate", birthDate);
userRef.updateChildren(userMap).addOnCompleteListener(new OnCompleteListener() {
@Override
public void onComplete(@NonNull Task task) {
if(task.isSuccessful()){
sendUserHomeActivity();
}
else{
Toast.makeText(InitialAccountSetupActivity.this, "Error occured", Toast.LENGTH_SHORT).show();
}
}
});
}
private void sendUserHomeActivity() {
Intent intent = new Intent(InitialAccountSetupActivity.this, HomeActivity.class);
startActivity(intent);
}
2019-07-19 16:46:22.360 11701-11701 / android.example.firebasetest E / AndroidRuntime:FATAL EXCEPTION:main 流程:android.example.firebasetest,PID:11701 java.lang.RuntimeException:无法启动活动ComponentInfo {android.example.firebasetest / android.example.firebasetest.InitialAccountSetupActivity}:java.lang.NullPointerException:尝试调用虚拟方法'com.google.firebase.auth.FirebaseUser com。 google.firebase.auth.FirebaseAuth.getCurrentUser()”上的空对象引用 在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3260) 在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3396) 在android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83) 在android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) 在android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) 在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:2009) 在android.os.Handler.dispatchMessage(Handler.java:107) 在android.os.Looper.loop(Looper.java:214) 在android.app.ActivityThread.main(ActivityThread.java:7319) 在java.lang.reflect.Method.invoke(本机方法) 在com.android.internal.os.RuntimeInit $ MethodAndArgsCaller.run(RuntimeInit.java:492) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:934) 引起原因:java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“ com.google.firebase.auth.FirebaseUser com.google.firebase.auth.FirebaseAuth.getCurrentUser()” 在android.example.firebasetest.InitialAccountSetupActivity.onCreate(InitialAccountSetupActivity.java:42) 在android.app.Activity.performCreate(Activity.java:7783) 在android.app.Activity.performCreate(Activity.java:7772) 在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1299) 在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3235) 在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3396) 在android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83) 在android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) 在android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) 在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:2009) 在android.os.Handler.dispatchMessage(Handler.java:107) 在android.os.Looper.loop(Looper.java:214) 在android.app.ActivityThread.main(ActivityThread.java:7319) 在java.lang.reflect.Method.invoke(本机方法) 在com.android.internal.os.RuntimeInit $ MethodAndArgsCaller.run(RuntimeInit.java:492) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:934)