从 splash activity 移动到 main Activity 时,Intent 正在生成空对象引用
Intent is producing null object reference when moving from splash activity to main Activity
之前一切正常,现在当我 运行 我的应用程序开始产生此错误并且我的应用程序不断崩溃。
这个错误不断出现
android.content.Intent android.content.Intent.putExtra(java.lang.String, int)' 空对象引用
这是我的 Splash Activity 代码。
Oncreate方法代码如下
@Override
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;
}
}
//update by this
new Handler().postDelayed(() -> {
if(redirectingIntent != null && fragmentKey != null)
{
redirectingIntent.putExtra("fragmentKey", fragmentKey);
startActivity(redirectingIntent);
finish();
}
}, 3000);
首先,我会避免延迟处理程序,因为它容易出错;更确切地说,这段代码
new Handler().postDelayed(() -> {
redirectingIntent.putExtra("fragmentKey", fragmentKey);
startActivity(redirectingIntent);
finish();
}, 3000);
我会制作另一种方法来处理重定向并在需要时调用它
private void redirectToNextActivity(fragmentKey: Integer) {
if (redirectingIntent != null ) {
redirectingIntent.putExtra("fragmentKey", fragmentKey);
startActivity(redirectingIntent);
finish();
}
}
之前一切正常,现在当我 运行 我的应用程序开始产生此错误并且我的应用程序不断崩溃。
这个错误不断出现
android.content.Intent android.content.Intent.putExtra(java.lang.String, int)' 空对象引用
这是我的 Splash Activity 代码。
Oncreate方法代码如下
@Override 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;
}
}
//update by this
new Handler().postDelayed(() -> {
if(redirectingIntent != null && fragmentKey != null)
{
redirectingIntent.putExtra("fragmentKey", fragmentKey);
startActivity(redirectingIntent);
finish();
}
}, 3000);
首先,我会避免延迟处理程序,因为它容易出错;更确切地说,这段代码
new Handler().postDelayed(() -> {
redirectingIntent.putExtra("fragmentKey", fragmentKey);
startActivity(redirectingIntent);
finish();
}, 3000);
我会制作另一种方法来处理重定向并在需要时调用它
private void redirectToNextActivity(fragmentKey: Integer) {
if (redirectingIntent != null ) {
redirectingIntent.putExtra("fragmentKey", fragmentKey);
startActivity(redirectingIntent);
finish();
}
}