Parcelable遇到IOException,在Fragment中写入可序列化的对象

时间:2018-11-13 10:26:23

标签: android ioexception

我在logcat中遇到此异常:

Parcelable encountered IOException writing serializable object (name = com.user.ringersoft.vaccinedemo.DieseasModel)

VaccineFragment.java:

    package com.user.ringersoft.vaccinedemo;

    public class VaccineFragment extends Fragment{
        RecyclerView recyclerView;
        RequestQueue queue;
        List<DieseasModel> dieseasModelList;
        List<VaccineDetailsModel> vaccineDetailsModels;
        public VaccineFragment() {
            // Required empty public constructor
        }


        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            View view= inflater.inflate(R.layout.fragment_vaccine, container, false);
            queue= Volley.newRequestQueue(getActivity().getApplicationContext());
            recyclerView= view.findViewById(R.id.vaccine_recycler);

            recyclerView.setHasFixedSize(true);
            recyclerView.setLayoutManager(new LinearLayoutManager(getActivity().getApplicationContext()));



            dieseasModelList = new ArrayList<>();


            String url= "http://myselftinku27.000webhostapp.com/vaccine.php";

            JsonObjectRequest jsonObjectRequest= new JsonObjectRequest(Request.Method.GET, url, new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {

                    try {

                        JSONArray jsonArray= response.getJSONArray("server_response");
                        for (int i=0;i<jsonArray.length();i++)
                        {
                            vaccineDetailsModels=new ArrayList<>();
                            DieseasModel dieseasModel= new DieseasModel();
                            JSONObject jsonObject=jsonArray.getJSONObject(i);
                            Log.d("jsonTest",jsonObject.toString());

                            dieseasModel.setId(jsonObject.getString("id"));
                            dieseasModel.setdName(jsonObject.getString("d_name"));
                            dieseasModel.setDescription(jsonObject.getString("description"));

                            JSONArray vaccine= jsonObject.getJSONArray("vaccine");

                            for (int j=0;j<vaccine.length();j++)
                            {
                                VaccineDetailsModel vaccineDetailsModel=new VaccineDetailsModel();
                                Log.d("vaccineTest",vaccine.toString());
                                JSONObject vac=vaccine.getJSONObject(j);
                              //  Toast.makeText(getContext(),""+vac,Toast.LENGTH_LONG).show();
                                vaccineDetailsModel.setVaccine_name(vac.getString("vname"));
                                vaccineDetailsModel.setFirst(vac.getString("first"));
                                vaccineDetailsModel.setSecond(vac.getString("second"));
                                vaccineDetailsModel.setThird(vac.getString("third"));
                                vaccineDetailsModel.setFourth(vac.getString("fourth"));
                                vaccineDetailsModels.add(vaccineDetailsModel);
                                Log.d("dieases1",vaccineDetailsModel.getVaccine_name());
                            }

                            dieseasModel.setVaccineDetailsModels(vaccineDetailsModels);
                            Log.d("dieases",dieseasModel.toString());
                            dieseasModelList.add(dieseasModel);


                        }

                        FragmentManager fragmentManager=getFragmentManager();
                        VaccineAdapter adapter= new VaccineAdapter(getContext(),dieseasModelList,fragmentManager);
                        recyclerView.setAdapter(adapter);

                    } catch (JSONException e) {

                        e.printStackTrace();
                    }



                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Log.d("error",error.toString());
                }
            });
            queue.add(jsonObjectRequest);


            return view;
        }

    }

DieseasModel.java:

public class DieseasModel implements Serializable {
    private String id;
    private String dName;
    private String description;
    List<VaccineDetailsModel> vaccineDetailsModels;

    public DieseasModel(String id, String dName, String description,List<VaccineDetailsModel> vaccineDetailsModels) {


        this.id = id;
        this.dName = dName;
        this.description=description;
        this.vaccineDetailsModels = vaccineDetailsModels;
    }

    public DieseasModel() {

    }

    public String getId() {
        return id;
    }

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

    public String getdName() {
        return dName;
    }

    public void setdName(String dName) {
        this.dName = dName;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public List<VaccineDetailsModel> getVaccineDetailsModels() {
        return vaccineDetailsModels;
    }

    public void setVaccineDetailsModels(List<VaccineDetailsModel> vaccineDetailsModels) {
        this.vaccineDetailsModels = vaccineDetailsModels;
    }
}

VaccineDetailsModel.java:

package com.user.ringersoft.vaccinedemo;

public class VaccineDetailsModel {
    String vaccine_name;
    String first;
    String second;
    String third;
    String fourth;

    public VaccineDetailsModel() {
    }

    public VaccineDetailsModel(String vaccine_name, String first, String second, String third, String fourth) {
        this.vaccine_name = vaccine_name;
        this.first = first;
        this.second = second;
        this.third = third;
        this.fourth = fourth;
    }

    public String getVaccine_name() {
        return vaccine_name;
    }

    public void setVaccine_name(String vaccine_name) {
        this.vaccine_name = vaccine_name;
    }

    public String getFirst() {
        return first;
    }

    public void setFirst(String first) {
        this.first = first;
    }

    public String getSecond() {
        return second;
    }

    public void setSecond(String second) {
        this.second = second;
    }

    public String getThird() {
        return third;
    }

    public void setThird(String third) {
        this.third = third;
    }

    public String getFourth() {
        return fourth;
    }

    public void setFourth(String fourth) {
        this.fourth = fourth;
    }
}

这是我的logcat:

java.lang.RuntimeException: Parcelable encountered IOException writing serializable object (name = com.user.ringersoft.vaccinedemo.DieseasModel)
        at android.os.Parcel.writeSerializable(Parcel.java:1761)
        at android.os.Parcel.writeValue(Parcel.java:1709)
        at android.os.Parcel.writeArrayMapInternal(Parcel.java:777)
        at android.os.BaseBundle.writeToParcelInner(BaseBundle.java:1506)
        at android.os.Bundle.writeToParcel(Bundle.java:1181)
        at android.os.Parcel.writeBundle(Parcel.java:817)
        at android.support.v4.app.FragmentState.writeToParcel(FragmentState.java:124)
        at android.os.Parcel.writeTypedArray(Parcel.java:1401)
        at android.support.v4.app.FragmentManagerState.writeToParcel(FragmentManager.java:639)
        at android.os.Parcel.writeParcelable(Parcel.java:1730)
        at android.os.Parcel.writeValue(Parcel.java:1636)
        at android.os.Parcel.writeArrayMapInternal(Parcel.java:777)
        at android.os.BaseBundle.writeToParcelInner(BaseBundle.java:1506)
        at android.os.Bundle.writeToParcel(Bundle.java:1181)
        at android.app.IActivityManager$Stub$Proxy.activityStopped(IActivityManager.java:4617)
        at android.app.ActivityThread$StopInfo.run(ActivityThread.java:3998)
        at android.os.Handler.handleCallback(Handler.java:789)
        at android.os.Handler.dispatchMessage(Handler.java:98)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6541)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
     Caused by: java.io.NotSerializableException: com.user.ringersoft.vaccinedemo.VaccineDetailsModel
        at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1233)
        at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:347)
        at java.util.ArrayList.writeObject(ArrayList.java:762)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.io.ObjectStreamClass.invokeWriteObject(ObjectStreamClass.java:977)
        at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1545)
        at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1481)
        at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1227)
        at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1597)
        at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1558)
        at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1481)
        at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1227)
        at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:347)
        at android.os.Parcel.writeSerializable(Parcel.java:1756)
        at android.os.Parcel.writeValue(Parcel.java:1709) 
        at android.os.Parcel.writeArrayMapInternal(Parcel.java:777) 
        at android.os.BaseBundle.writeToParcelInner(BaseBundle.java:1506) 
        at android.os.Bundle.writeToParcel(Bundle.java:1181) 
        at android.os.Parcel.writeBundle(Parcel.java:817) 
        at android.support.v4.app.FragmentState.writeToParcel(FragmentState.java:124) 
        at android.os.Parcel.writeTypedArray(Parcel.java:1401) 
        at android.support.v4.app.FragmentManagerState.writeToParcel(FragmentManager.java:639) 
        at android.os.Parcel.writeParcelable(Parcel.java:1730) 
        at android.os.Parcel.writeValue(Parcel.java:1636) 
        at android.os.Parcel.writeArrayMapInternal(Parcel.java:777) 
        at android.os.BaseBundle.writeToParcelInner(BaseBundle.java:1506) 
        at android.os.Bundle.writeToParcel(Bundle.java:1181) 
        at android.app.IActivityManager$Stub$Proxy.activityStopped(IActivityManager.java:4617) 
        at android.app.ActivityThread$StopInfo.run(ActivityThread.java:3998) 
        at android.os.Handler.handleCallback(Handler.java:789) 
        at android.os.Handler.dispatchMessage(Handler.java:98) 
        at android.os.Looper.loop(Looper.java:164) 
        at android.app.ActivityThread.main(ActivityThread.java:6541) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767) 

1 个答案:

答案 0 :(得分:0)

您的VaccineDetailsModel必须实现为可序列化