1

[Error]

Fatal Exception: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mypackage/folders.views.activity.MainActivity$17}: 
android.os.BadParcelableException: Parcelable protocol requires a Parcelable.Creator object called CREATOR on class 

Im getting the BadParcelableException from the following code but have no clue why I'm getting this. I'm calling this code in onCreateView of a Fragment. The mysterious part is that it doesn't crash all the time.I'm totally helpless now. If you have some tips or example, I would love o hear from you!

final BigItem bigItem = getArguments().getParcelable("big_item"); ← I get error at this code

I'm putting the parcelable in the budle from this function, but I don't think this is the cause.

public static BigFragment newInstance(@NonNull final BigItem bigItem, @NonNull final BigListener eventListener) {
        final BigFragment frag = new BigFragment();
        Bundle args = new Bundle();
        args.putParcelable("big_item", bigItem);
        frag.setArguments(args);
        return frag;
}

And this is the code for BigItem is as follows.

public class BigItem implements Parcelable {


    public long id;

    public String image;

    public String text;

    public String web;

    public BigItem() {

    }

    protected BigItem(Parcel in) {
        id = in.readLong();
        image = in.readString();
        text = in.readString();
        web = in.readString();
    }

    public static final Creator<BigItem> CREATOR = new Creator<BigItem>() {
        @Override
        public BigItem createFromParcel(Parcel in) {
            return new BigItem(in);
        }

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

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

    @Override
    public void writeToParcel(Parcel par, int flags) {
        par.writeLong(id);
        par.writeString(image);
        par.writeString(text);
        par.writeString(web);
    }
}

The following method with FragmentTransaction is in my MainActivity

private void addBigFragment(@NonNull final BigItem bigItem, @NonNull final BigFragment.BigEventListener eventListener) {
        final FragmentManager fragmentManager = getSupportFragmentManager();
        rmBigFragment(fragmentManager);

        final FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        mBigFragment = BigFragment.newInstance(bigItem, eventListener);
        fragmentTransaction.replace(R.id.main_big_layout_fragment, mBigFragment, BigFragment.TAG);
        fragmentTransaction.commit();
}
Nancy
  • 129
  • 2
  • 13
  • possible duplicated [Android: Making a class parcelable](https://stackoverflow.com/questions/8186023/android-making-a-class-parcelable) – JunJie Wang Aug 30 '18 at 01:52
  • Thank you, but the above link shows how to make a parcelable class, and the error I'm getting might not be related to it. – Nancy Aug 30 '18 at 01:55
  • Are you putting any _other_ parcelable objects into that same Bundle? The `BadParcelableException` is thrown when the system determines that the parceled data is out of sync, but that can be caused by bad parceling/deparceling of an earlier object. – Ben P. Aug 30 '18 at 01:57
  • can you upload the snippet about the calling of FragmentTransaction, newInstance? – JunJie Wang Aug 30 '18 at 02:53
  • [you must call addToBackStack() before you commit the FragmentTransaction.](http://sapandiwakar.in/replacing-fragments) and wonder why are you like use "final" identifier. (+1) – JunJie Wang Aug 30 '18 at 03:41
  • Thanks, do you think adding addToBacStack will solve this issue? If so why? – Nancy Aug 31 '18 at 02:16

1 Answers1

0

possible duplicated with this article : How to use Parcelable in fragment for getting data?

I saw your code, did not found error. You said "it doesn't crash all the time", make me understand you did not insert to exception. if am i, i will write;

Bundle bundle = getArguments();
if (bundle != null) {
    BigItem bigItem = bundle.getParcelable("big_item");
}

because sometimes fragment will be resume without arguments.

JunJie Wang
  • 460
  • 3
  • 10