1

I am getting BadParcelableException in my application on passing parcelable values using intent action. How to pass parcelable values and retrieve them in another classes?

Below is my code:

public MySample mySample;

  public class MySample implements Parcelable {
        private boolean galleryPicker = false;
        private boolean gallery = false;
        private ImageButton button;
        private View view;

        public ImageButton getButton() {
            return button;
        }

        public void setPostButtonItem(ImageButton button) {
            this.button= button;
        }
        @Override
        public void writeToParcel(Parcel dest, int flags) {
            dest.writeStringArray(new String[] {});
        }

        public int describeContents(){
            return 0;
        }

Intent action:

public class Popup// (Actually I call this class from my BaseActivity)
{
    public Popup(Activity activity,ActionBar action)
    {
        MySample mySample= new MySample();
        sample.data.setStatus=true;
        no= (TextView) popupView.findViewById(R.id.no);
        yes= (TextView) popupView.findViewById(R.id.yes);
        mySample.gallery = true;
        mySample.count = true;
        context = getContext();
        Intent intent = new Intent(context, NextActivity.class);
        intent.putExtra("sample", mySample);
        context.startActivity(intent);
    }
}

Extract intent in another class:

private MySample mySample;
Bundle data = getIntent().getExtras();
this.mySample= data.getParcelable("sample");

Exception:

Caused by: android.os.BadParcelableException: Parcelable protocol requires a Parcelable.Creator object called CREATOR on class

Cœur
  • 37,241
  • 25
  • 195
  • 267
Vicky
  • 921
  • 1
  • 11
  • 33
  • Your problem is not in passing the value. Your problem is that you did not fully implement `Parcelable`. In particular, quoting [the documentation](https://developer.android.com/reference/android/os/Parcelable.html): "Classes implementing the Parcelable interface must also have a static field called CREATOR, which is an object implementing the Parcelable.Creator interface. " – CommonsWare Mar 05 '15 at 13:19
  • let me try it and can you wait for my response – Vicky Mar 05 '15 at 13:26

1 Answers1

2

Try this

 public class MySample implements Parcelable {

    public static final Parcelable.Creator CREATOR = new Parcelable.Creator() {
        public MySample createFromParcel(Parcel in) {
            return new MySample(in);
        }

        public MySample[] newArray(int size) {
            return new MySample[size];
        }
    };
    private boolean galleryPicker = false;
    private boolean gallery = false;

    public MySample(Parcel in) {
        boolean[] temp = new boolean[2];
        in.readBooleanArray(temp);
        galleryPicker = temp[0];
        gallery = temp[1];
    }

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

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeBooleanArray(new boolean[]{galleryPicker, gallery});
    }
}
tebitoq
  • 165
  • 1
  • 13