【发布时间】:2020-09-01 10:30:03
【问题描述】:
以前一切正常,现在当我运行我的应用程序时,它开始产生此错误并且我的应用程序不断崩溃。
此错误不断出现
android.content.Intent android.content.Intent.putExtra(java.lang.String, int)' 在空对象引用上
这是我的 Splash 活动代码。
Oncreate 方法代码如下
@覆盖 protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
FirebaseApp.initializeApp(SplashActivity.this);
fragmentKey=0;
intiViews();
new Handler().postDelayed(() -> {
redirectingIntent.putExtra("fragmentKey", fragmentKey);
startActivity(redirectingIntent);
finish();
}, 3000);
InitViews 方法代码如下
private void intiViews() {
userRef = FirebaseDatabase.getInstance().getReference(Common.USER_REF);
mAuth = FirebaseAuth.getInstance();
//Check User if it exist
if (mAuth.getCurrentUser() != null) {
user = mAuth.getCurrentUser();
CheckUserFromFirebase(user);
} else {
redirectingIntent = new Intent(SplashActivity.this, RegisterActivity.class);
fragmentKey = 0;
}
}
private void CheckUserFromFirebase(final FirebaseUser user) {
userRef.child(user.getUid()).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()) {
UserModel userModel = dataSnapshot.getValue(UserModel.class);
updateUser(userModel);
redirectingIntent = new Intent(SplashActivity.this, MainActivity.class);
fragmentKey = 0;
} else {
RegisterUser();
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Toast.makeText(SplashActivity.this, databaseError.getMessage(), Toast.LENGTH_SHORT).show();
}
});
}
在这里,我正在使用上传到 firebase 的用户模型更新我的用户
private void updateUser(UserModel model) {
Common.CurrentUser = model;
Common.CurrentUser.setUid(user.getUid());
}
如果没有给定uid的注册用户,则intent会去注册activity
private void RegisterUser() {
Toast.makeText(this, "No Data found", Toast.LENGTH_SHORT).show();
redirectingIntent = new Intent(SplashActivity.this, RegisterActivity.class);
fragmentKey = 1;
}
}
【问题讨论】:
-
我已经应用了无效捕获并重新启动,但仍然没有结果。
-
redirectingIntent未初始化。检查您的条件并确保在处理程序执行之前初始化redirectingIntent。 -
感谢您的帮助,错误现已消除。
标签: android android-studio android-intent