[错误]
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
我从以下代码中得到了 BadParcelableException,但不知道为什么会得到这个。我在片段的 onCreateView 中调用此代码。神秘的是它不会一直崩溃。我现在完全无能为力了。如果您有一些提示或示例,我很乐意收到您的来信!
final BigItem bigItem = getArguments().getParcelable("big_item"); ← I get error at this code
我将这个功能的包裹放在了 budle 中,但我认为这不是原因。
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;
}
这是 BigItem 的代码如下。
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);
}
}
以下带有 FragmentTransaction 的方法在我的 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();
}