java getIntent()。getSerializableExtra为空
我有一个POJO
类实现Serializable
我想通过帮助Intent
和Bundle
将这个类的一个对象转移到另一个activity
。
我在传输之前检查了对象,它不是空的。(正确获取其中一个属性)
put:
private void onFriendClick(FriendHolder holder, int position) {
Intent intent = new Intent(context, ProfileActivity.class);
Bundle extra = new Bundle();
extra.putSerializable(Consts.KEY_USER_JSON, friendList.get(position));
intent.putExtra(Consts.KEY_USER_JSON, extra);
Log.e("onFriendClick", String.valueOf(friendList.get(position).getName()));
context.startActivity(intent);
}
从另一项活动中获取:
private void setupProfile() {
Bundle extras = getIntent().getExtras();
if (extras != null) {
profile = (ProfileDTO) getIntent().getSerializableExtra(Consts.KEY_USER_JSON);
Log.e("onFriendClick", String.valueOf(profile.getName()));//NPE this
} else profile = user.getProfile();
}
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String ru.techmas.getmeet.api.models.ProfileDTO.getName()' on a null object reference at ru.techmas.getmeet.activities.ProfileActivity.setupJsonProfile(ProfileActivity.java:103)
# 1 楼答案
您不需要创建
Bundle
。 试着做:但是如果仍然想使用
Bundle
,那么应该在发布的代码中替换:作者: