Java getIntent().getSerializableExtra为空

Java getIntent().getSerializableExtra为空,java,android,android-intent,bundle,Java,Android,Android Intent,Bundle,我有一个POJO类实现了Serializable 我想通过帮助Intent和Bundle将此类的一个对象转移到另一个activity。 我在传输之前检查了对象,它不是空的。(正确获取其中一个属性) put: private void onFriendClick(FriendHolder holder, int position) { Intent intent = new Intent(context, ProfileActivity.class); Bundle

我有一个
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();
    }
从其他活动获取:

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();
    }
原因:java.lang.NullPointerException:尝试调用虚拟机 方法“java.lang.String” null对象上的ru.techmas.getmeet.api.models.ProfileDTO.getName() 参考 在ru.techmas.getmeet.activities.ProfileActivity.setupJsonProfile(ProfileActivity.java:103)


您不需要创建
捆绑包
。 试着做:

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();
    }
}

但如果仍要使用
捆绑包
,则应在发布的代码中替换:

profile = (ProfileDTO) getIntent().getSerializableExtra(Consts.KEY_USER_JSON);
作者: