getIntent().getSerializableExtra 为空
getIntent().getSerializableExtra is null
我有一个 POJO
class 工具 Serializable
我想在 Intent
和 Bundle
的帮助下将这个 class 的对象转移到另一个 activity
。
我在传输之前检查了对象,它不是空的。 (正确获取其中一个属性)
放:
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);
}
从另一个获取activity:
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)
您不需要创建 Bundle
。
尝试做:
private void onFriendClick(FriendHolder holder, int position) {
Intent intent = new Intent(context, ProfileActivity.class);
intent.putExtra(Consts.KEY_USER_JSON, friendList.get(position));
Log.e("onFriendClick", String.valueOf(friendList.get(position).getName()));
context.startActivity(intent);
}
private void setupProfile() {
if (getIntent().getSerializableExtra(Consts.KEY_USER_JSON) != null) {
profile = (ProfileDTO) getIntent().getSerializableExtra(Consts.KEY_USER_JSON);
Log.e("onFriendClick", String.valueOf(profile.getName()));//NPE this
} else {
profile = user.getProfile();
}
}
但如果仍想使用 Bundle
,您应该替换您发布的代码:
profile = (ProfileDTO) getIntent().getSerializableExtra(Consts.KEY_USER_JSON);
作者:
profile = (ProfileDTO) extras.getSerializableExtra(Consts.KEY_USER_JSON);
我有一个 POJO
class 工具 Serializable
我想在 Intent
和 Bundle
的帮助下将这个 class 的对象转移到另一个 activity
。
我在传输之前检查了对象,它不是空的。 (正确获取其中一个属性)
放:
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);
}
从另一个获取activity:
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)
您不需要创建 Bundle
。
尝试做:
private void onFriendClick(FriendHolder holder, int position) {
Intent intent = new Intent(context, ProfileActivity.class);
intent.putExtra(Consts.KEY_USER_JSON, friendList.get(position));
Log.e("onFriendClick", String.valueOf(friendList.get(position).getName()));
context.startActivity(intent);
}
private void setupProfile() {
if (getIntent().getSerializableExtra(Consts.KEY_USER_JSON) != null) {
profile = (ProfileDTO) getIntent().getSerializableExtra(Consts.KEY_USER_JSON);
Log.e("onFriendClick", String.valueOf(profile.getName()));//NPE this
} else {
profile = user.getProfile();
}
}
但如果仍想使用 Bundle
,您应该替换您发布的代码:
profile = (ProfileDTO) getIntent().getSerializableExtra(Consts.KEY_USER_JSON);
作者:
profile = (ProfileDTO) extras.getSerializableExtra(Consts.KEY_USER_JSON);