通过intent传递自定义对象(Parcelable)返回null

时间:2018-01-29 07:36:25

标签: java android android-intent

我已经通过auto-plugin生成了Parcelable类,所以我无法真正认为CustomUser类代码中存在错误,无论我如何在意图中传递它并尝试接收它,我仍然得到零。 我做错了什么? 自动生成是否还需要额外的修改?

public class CustomUser implements Parcelable {

    public CustomUser() {
    }

    public CustomUser(String age, String date_of_birth, String description, String name, String photo_link, String sex) {
        this.age = age;
        this.date_of_birth = date_of_birth;
        this.description = description;
        this.name = name;
        this.photo_link = photo_link;
        this.sex = sex;
        this.defaultGender = defaultGender;
    }

    private String age;
    private String date_of_birth;
    private String description;
    private String uid;



    public String getUid() {
        return uid;
    }

    public void setUid(String uid) {
        this.uid = uid;
    }

    private String name;
    private String photo_link;
    private String sex;
    private String[] defaultGender = {"default_gender"};

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAge() {
        return age;
    }

    public void setAge(String age) {
        this.age = age;
    }

    public String getDate_of_birth() {
        return date_of_birth;
    }

    public void setDate_of_birth(String date_of_birth) {
        this.date_of_birth = date_of_birth;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getPhoto_link() {
        return photo_link;
    }

    public void setPhoto_link(String photo_link) {
        this.photo_link = photo_link;
    }


    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(this.age);
        dest.writeString(this.date_of_birth);
        dest.writeString(this.description);
        dest.writeString(this.uid);
        dest.writeString(this.name);
        dest.writeString(this.photo_link);
        dest.writeString(this.sex);
        dest.writeStringArray(this.defaultGender);
    }

    protected CustomUser(Parcel in) {
        this.age = in.readString();
        this.date_of_birth = in.readString();
        this.description = in.readString();
        this.uid = in.readString();
        this.name = in.readString();
        this.photo_link = in.readString();
        this.sex = in.readString();
        this.defaultGender = in.createStringArray();
    }

    public static final Parcelable.Creator<CustomUser> CREATOR = new Parcelable.Creator<CustomUser>() {
        @Override
        public CustomUser createFromParcel(Parcel source) {
            return new CustomUser(source);
        }

        @Override
        public CustomUser[] newArray(int size) {
            return new CustomUser[size];
        }
    };
}

MainActivity.class

customUser = documentSnapshot.toObject(CustomUser.class);
Log.d(TAG, "CustomUser not null : " + customUser.getName());  // its OK
Intent intent = new Intent(getApplicationContext(),SearchActivity.class);
intent.putExtra("parcelable",customUser);
startActivity(intent);

SearchActivity.class

 Intent i = getIntent();
CustomUser customUser = i.getParcelableExtra("parcelable");
Log.d(TAG, "onCreate123123: "+customUser.getName());

2 个答案:

答案 0 :(得分:1)

使用Gson将Object转换为String,将String转换为Object: 图书馆:$ hg help | grep branch`

branch        set or show the current branch name  
branches      list repository named branches  
graft         copy changes from other branches onto the current branch  
heads         show branch heads  

答案 1 :(得分:0)

用以下代码替换您的代码,

您需要投射宗地对象。

lapply