Nancy
Nancy

Reputation: 139

getParcelable from Bundle sometimes causes BadParcelableException in java android

[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();
}

Upvotes: 1

Views: 606

Answers (1)

JunJie Wang
JunJie Wang

Reputation: 470

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.

Upvotes: 0

Related Questions