Reputation: 10948
I usually have no problem with parcelable, but this time i got NPE...and i cant fix it. Please kindly help me.
The NPE occurred only when i try to pass the parcelable class from fragment to another activity. Things working fine if i pass the parcelable class from activity to another activity or from activity to fragment.
This is my fragment code, the child
(parcelable class) is NOT null when i debugged it :
if(v == imgChild)
{
Intent i = new Intent(getActivity(), DetailChildActivity.class);
i.putExtra("child", child);
startActivity(i);
Log.d("menu", "children");
}
Here in my target onCreate
activity :
Bundle data = getIntent().getExtras();
if(data != null)
{
child = data.getParcelable("child"); //NPE here
txtName.setText(child.getName());
}
This is strange. The data is NOT null, but the parcelable is always null.
This is my parcelable (Child
) class :
public final Parcelable.Creator<Child> CREATOR = new Parcelable.Creator<Child>() {
public Child createFromParcel(Parcel in) {
return new Child(in);
}
public Child[] newArray(int size) {
return new Child[size];
}
};
private Child(Parcel in) {
id = in.readInt();
user_id = in.readInt();
name = in.readString();
gender = in.readString();
born_date = in.readString();
born_hour = in.readString();
hospital = in.readString();
result = in.readString();
}
@Override
public int describeContents() {
// TODO Auto-generated method stub
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
// TODO Auto-generated method stub
dest.writeInt(id);
dest.writeInt(user_id);
dest.writeString(name);
dest.writeString(gender);
dest.writeString(born_date);
dest.writeString(born_hour);
dest.writeString(hospital);
dest.writeString(result);
}
This is what the log cat
said :
expected receiver of type com.blabla.child, but got null
Thanks for your time.
Upvotes: 0
Views: 739
Reputation: 10948
Strange. I know the answer, but i dont understand why. Its from FragmentActivity NullPointer in onCreate savedInstanceState Bundle :
public static final Parcelable.Creator
The creator should be in static and final, not just final. Thats it.
Maybe you can explain to me how this work, this is the answer for now.
Upvotes: 1