Home » Android » java – Parcelable object is Null on receiving activity and good in other-Exceptionshub

java – Parcelable object is Null on receiving activity and good in other-Exceptionshub

Posted by: admin February 24, 2020 Leave a comment

Questions:

Thanks to those who will try to help me!

First time I sent a message. I’m stuck with my parcel.
I try to send an object Neighbour in a recyclerView for a class DetailedNeighbour. After I send my variable to DetailedActivity.putExtra("DNeighbour", neighbour); is well equal to what I want and after in DetailedNeighbourActivity is equal to null.

Neighbour:

package com.openclassrooms.entrevoisins.model;

/**
 * Model object representing a Neighbour
 */
public class Neighbour implements Parcelable {

    /** Identifier */
    private long id;

    /** Full name */
    private String name;

    /** Avatar */
    private String avatarUrl;

    /** Adress */
    private String address;

    /** Phone number */
    private String phoneNumber;

    /** About me */
    private String aboutMe;

    /**
     * Constructor
     * @param id
     * @param name
     * @param avatarUrl
     */
    public Neighbour(long id, String name, String avatarUrl, String address,
                     String phoneNumber, String aboutMe) {
        this.id = id;
        this.name = name;
        this.avatarUrl = avatarUrl;
        this.address = address;
        this.phoneNumber = phoneNumber;
        this.aboutMe = aboutMe;
    }

        public  Neighbour (Parcel in){   //constructor  //Protected ?
        id =in.readLong();                  //read and set saved values from parcel
        name=in.readString();
        avatarUrl=in.readString();
        address=in.readString();
        phoneNumber=in.readString();
        aboutMe=in.readString();
    }

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

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

    public String getAvatarUrl() {
        return avatarUrl;
    }

    public void setAvatarUrl(String avatarUrl) {
        this.avatarUrl = avatarUrl;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getPhoneNumber() {
        return phoneNumber;
    }

    public void setPhoneNumber(String phoneNumber) {
        this.phoneNumber = phoneNumber;
    }

    public String getAboutMe() {
        return aboutMe;
    }

    public void setAboutMe(String aboutMe) {
        this.aboutMe = aboutMe;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Neighbour neighbour = (Neighbour) o;
        return Objects.equals(id, neighbour.id);
    }

    @Override
    public int hashCode() {
        return Objects.hash(id);
    }

    @Override
    public void writeToParcel(Parcel parcel, int flags) { // In this method you add all your class properties to the parcel which are needed to transfer.
        parcel.writeString(name);
        parcel.writeString(phoneNumber);
        parcel.writeString(avatarUrl);
        parcel.writeString(aboutMe);
        parcel.writeString(address);
        parcel.writeLong(id);  /// for favoris ?

    }

    public static final Parcelable.Creator<Neighbour> CREATOR = new Parcelable.Creator<Neighbour>() {   ///This is the method which is used to bind everything together. Nothing much is done here.
        @Override
        public Neighbour createFromParcel(Parcel in) {
            return new Neighbour(in);
        }




        @Override
        public Neighbour[] newArray(int size) {
            return new Neighbour[size];
        }
    };
    public static Creator<Neighbour> getCREATOR() {
        return CREATOR;
    }
    @Override
    public int describeContents() {
        return 0;
    }
}

RecyclerViewAdapter:

package com.openclassrooms.entrevoisins.ui.neighbour_list;

public class MyNeighbourRecyclerViewAdapter extends RecyclerView.Adapter<MyNeighbourRecyclerViewAdapter.ViewHolder> {

    private final List<Neighbour> mNeighbours;

    public MyNeighbourRecyclerViewAdapter(List<Neighbour> items) {
        mNeighbours = items;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.fragment_neighbour, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, int position) {
        Neighbour neighbour = mNeighbours.get(position);
        holder.mNeighbourName.setText(neighbour.getName());
        Glide.with(holder.mNeighbourAvatar.getContext())
                .load(neighbour.getAvatarUrl())
                .apply(RequestOptions.circleCropTransform())
                .into(holder.mNeighbourAvatar);
        holder.mNeighbourName.setOnClickListener(new View.OnClickListener() { ///   Observe le clic sur bouton name
            @Override
            public void onClick(View view) {
                Intent DetailedActivity = new Intent(view.getContext(), DetailedNeighbourActivity.class);       //
                DetailedActivity.putExtra("DNeighbour", neighbour);
                view.getContext().startActivity(DetailedActivity);//
                EventBus.getDefault();//
                Log.i("DEBUG","l'utilisateur essaye d'ouvrir le détaille");
            }
        });
        holder.mDeleteButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                EventBus.getDefault().post(new DeleteNeighbourEvent(neighbour));
            }
        });
    }

    @Override
    public int getItemCount() {
        return mNeighbours.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.item_list_avatar)
        public ImageView mNeighbourAvatar;
        @BindView(R.id.item_list_name)
        public TextView mNeighbourName;
        @BindView(R.id.item_list_delete_button)
        public ImageButton mDeleteButton;

        public ViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }
}

DetailedNeighboursActivity:


public class DetailedNeighbourActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_detailed_neighbour);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        Intent intent = getIntent();
        Neighbour neighbour = (Neighbour) getIntent().getParcelableExtra("DNeighbour");
        String name = neighbour.getName();


        EventBus.getDefault();
        Log.i("DEBUG", "Le détaille "+ name);
    }
}
How to&Answers:

I’m not really smart ! Sorry for you time .
parcel and writeparcel constructor need the same order for variables. it’s ok my code work.