Parcelable 对象在接收 activity 时为 Null,在其他情况下为 Good
Parcelable object is Null on receiving activity and good in other
感谢那些愿意帮助我的人!
第一次发消息。我被包裹困住了。
我尝试在 class DetailedNeighbour
的 recyclerView 中发送对象 Neighbor。在我将我的变量发送到 DetailedActivity.putExtra("DNeighbour", neighbour);
之后等于我想要的并且在 DetailedNeighbourActivity
之后等于 null.
邻居:
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);
}
}
我真的不聪明!对不起你的时间。
parcel 和 writeparcel 构造函数需要相同的变量顺序。没关系我的代码工作。
感谢那些愿意帮助我的人!
第一次发消息。我被包裹困住了。
我尝试在 class DetailedNeighbour
的 recyclerView 中发送对象 Neighbor。在我将我的变量发送到 DetailedActivity.putExtra("DNeighbour", neighbour);
之后等于我想要的并且在 DetailedNeighbourActivity
之后等于 null.
邻居:
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);
}
}
我真的不聪明!对不起你的时间。 parcel 和 writeparcel 构造函数需要相同的变量顺序。没关系我的代码工作。