在fragment 传递数据的时候报:Parcel: unable to marshal value

来源:互联网 发布:刘项原来不读书 知乎 编辑:程序博客网 时间:2024/04/30 17:38

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;}

@Overridepublic 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];    }};@Overridepublic void writeToParcel(Parcel dest, int flags) {    dest.writeList(category);    dest.writeList(recommend);}


0 0