天天看點

在fragment 傳遞資料的時候報:Parcel: unable to marshal value

public static FragmentLiveShowContList newInstance(Context context, T_LiveShowTitle liveShows, String id) {
    mContext = context;
    FragmentLiveShowContList fragment = new FragmentLiveShowContList();
    Bundle bundle = new Bundle();
    bundle.putParcelable("liveshow",liveShows);
    bundle.putString("id",id);
    fragment.setArguments(bundle);
    return fragment;
}      
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    if (getArguments() != null) {
        liveShow = getArguments().getParcelable("liveshow");
        mId = getArguments().getString("id");
    }      

}

在fragment 傳遞資料的時候報:Parcel: unable to marshal value 

 FATAL EXCEPTION: main

                                                                    Process: com.cmcc.migutvtwo, PID: 24837

                                                                    java.lang.RuntimeException: Parcel: unable to marshal value T_LiveShowTitle{id='1', name='推薦'}

                                                                        at android.os.Parcel.writeValue(Parcel.java:1337)

                                                                        at android.os.Parcel.writeList(Parcel.java:711)

                                                                        at com.cmcc.migutvtwo.model.T_LiveShow.writeToParcel(T_LiveShow.java:63)

                                                                        at android.os.Parcel.writeParcelable(Parcel.java:1357)

                                                                        at android.os.Parcel.writeValue(Parcel.java:1262)

                                                                        at android.os.Parcel.writeArrayMapInternal(Parcel.java:638)

                                                                        at android.os.BaseBundle.writeToParcelInner(BaseBundle.java:1313)

                                                                        at android.os.Bundle.writeToParcel(Bundle.java:1096)

                                                                        at android.os.Parcel.writeBundle(Parcel.java:663)

                                                                        at android.support.v4.app.FragmentState.writeToParcel(Fragment.java:137)

                                                                        at android.os.Parcel.writeTypedArray(Parcel.java:1191)

                                                                        at android.support.v4.app.FragmentManagerState.writeToParcel(FragmentManager.java:384)

                                                                        at android.os.Parcel.writeParcelable(Parcel.java:1357)

                                                                        at android.os.Parcel.writeValue(Parcel.java:1262)

                                                                        at android.os.Parcel.writeArrayMapInternal(Parcel.java:638)

                                                                        at android.os.BaseBundle.writeToParcelInner(BaseBundle.java:1313)

                                                                        at android.os.Bundle.writeToParcel(Bundle.java:1096)

                                                                        at android.os.Parcel.writeBundle(Parcel.java:663)

                                                                        at android.support.v4.app.FragmentState.writeToParcel(Fragment.java:138)

                                                                        at android.os.Parcel.writeTypedArray(Parcel.java:1191)

                                                                        at android.support.v4.app.FragmentManagerState.writeToParcel(FragmentManager.java:384)

                                                                        at android.os.Parcel.writeParcelable(Parcel.java:1357)

                                                                        at android.os.Parcel.writeValue(Parcel.java:1262)

                                                                        at android.os.Parcel.writeArrayMapInternal(Parcel.java:638)

                                                                        at android.os.BaseBundle.writeToParcelInner(BaseBundle.java:1313)

                                                                        at android.os.Bundle.writeToParcel(Bundle.java:1096)

                                                                        at android.os.Parcel.writeBundle(Parcel.java:663)

                                                                        at android.app.ActivityManagerProxy.activityStopped(ActivityManagerNative.java:2922)

                                                                        at android.app.ActivityThread$StopInfo.run(ActivityThread.java:3346)

                                                                        at android.os.Handler.handleCallback(Handler.java:739)

                                                                        at android.os.Handler.dispatchMessage(Handler.java:95)

                                                                        at android.os.Looper.loop(Looper.java:135)

                                                                        at android.app.ActivityThread.main(ActivityThread.java:5310)

                                                                        at java.lang.reflect.Method.invoke(Native Method)

                                                                        at java.lang.reflect.Method.invoke(Method.java:372)

                                                                        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:901)

                                                                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696)

是因為我的T_LiveShowTitle 沒有實作 Parcelable 的接口,而T_LiveShowTitle 父類實作了Parcelable 的接口,是以導緻報錯

讓它T_LiveShowTitle 實作Parcelable的接口就可以了 :

import android.os.Parcel;
import android.os.Parcelable;


public class T_LiveShowTitle  implements Parcelable {

    private String id;
    private String name;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "T_LiveShowTitle{" +
                "id='" + id + '\'' +
                ", name='" + name + '\'' +
                '}';
    }

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

    Parcelable.Creator<T_LiveShowTitle> CREATOR = new Creator<T_LiveShowTitle>() {
        @Override
        public T_LiveShowTitle createFromParcel(Parcel source) {
            T_LiveShowTitle liveshow = new T_LiveShowTitle();
            liveshow.id = source.readString();
            liveshow.name = source.readString();
            return liveshow;
        }

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

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(id);
        dest.writeString(name);
    }
}
      

另外在fragment 傳遞list的資料時 必須是ArrayList,否則就會報Parcel: unable to marshal value 這個問題

List的實作方式:

private List<T_LiveShowTitle> category;      
Parcelable.Creator<T_LiveShow> CREATOR = new Creator<T_LiveShow>() {
    @Override
    public T_LiveShow createFromParcel(Parcel source) {
        T_LiveShow liveshow = new T_LiveShow();
        liveshow.category = source.readArrayList(T_LiveShowTitle.class.getClassLoader());
        liveshow.recommend = source.readArrayList(T_LiveshowCont.class.getClassLoader());
        return liveshow;
    }

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

@Override
public void writeToParcel(Parcel dest, int flags) {
    dest.writeList(category);
    dest.writeList(recommend);
}