Parcelable encountered IOException writing seriali

2019-06-11 12:17发布

问题:

I have an app that contains a MainActivity that contains a tabbed view and hosts a different fragment for each tab. This mechanism works trough a SectionPageAdapter that extends a FragmentPageAdapter.

One of the tabs brings out a GeneralSearchFragment

public class GeneralSearchFragment extends Fragment {

public GeneralSearchFragment(){

}

@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
    final ConstraintLayout constraintLayout = (ConstraintLayout) inflater.inflate(R.layout.fragment_searchtab, container, false);


    ((ImageView)constraintLayout.findViewById(R.id.searchByName)).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent = new Intent();
            intent.setClass(getActivity(), FreeSearchActivity.class);
            startActivity(intent);
         }
    });

    return constraintLayout;
}

}

As I click on an image, my code should start another different Activity. In my code I don't pass anything to the Activity, so I don't understand where the serializing error is coming from. The only time I serialize something is when I pass a list of items to a fragment trough a bundle. But how is that connected to what is happening here ?

Thanks a bunch

Btw I understand the problem the logcat is pointing out if I was in fact trying to serialize it here. The only time I serialize that object ( that in fact contains a drawable ) is on another fragment and it works fine. It gives me no error. The drawable btw comes from resources

Logcat

12-26 08:17:52.853 5129-5129/com.chronocrops.chronocropps E/AndroidRuntime: FATAL EXCEPTION: main
                                                                        Process: com.chronocrops.chronocropps, PID: 5129
                                                                        java.lang.RuntimeException: Parcelable encountered IOException writing serializable object (name = com.chronocrops.chronocropps.Model.Crop)
                                                                            at android.os.Parcel.writeSerializable(Parcel.java:1822)
                                                                            at android.os.Parcel.writeValue(Parcel.java:1770)
                                                                            at android.os.Parcel.writeList(Parcel.java:926)
                                                                            at android.os.Parcel.writeValue(Parcel.java:1719)
                                                                            at android.os.Parcel.writeArrayMapInternal(Parcel.java:838)
                                                                            at android.os.BaseBundle.writeToParcelInner(BaseBundle.java:1542)
                                                                            at android.os.Bundle.writeToParcel(Bundle.java:1232)
                                                                            at android.os.Parcel.writeBundle(Parcel.java:878)
                                                                            at android.support.v4.app.FragmentState.writeToParcel(Fragment.java:157)
                                                                            at android.os.Parcel.writeTypedArray(Parcel.java:1462)
                                                                            at android.support.v4.app.FragmentManagerState.writeToParcel(FragmentManager.java:617)
                                                                            at android.os.Parcel.writeParcelable(Parcel.java:1791)
                                                                            at android.os.Parcel.writeValue(Parcel.java:1697)
                                                                            at android.os.Parcel.writeArrayMapInternal(Parcel.java:838)
                                                                            at android.os.BaseBundle.writeToParcelInner(BaseBundle.java:1542)
                                                                            at android.os.Bundle.writeToParcel(Bundle.java:1232)
                                                                            at android.app.IActivityManager$Stub$Proxy.activityStopped(IActivityManager.java:4604)
                                                                            at android.app.ActivityThread$StopInfo.run(ActivityThread.java:3934)
                                                                            at android.os.Handler.handleCallback(Handler.java:790)
                                                                            at android.os.Handler.dispatchMessage(Handler.java:99)
                                                                            at android.os.Looper.loop(Looper.java:164)
                                                                            at android.app.ActivityThread.main(ActivityThread.java:6494)
                                                                            at java.lang.reflect.Method.invoke(Native Method)
                                                                            at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
                                                                            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
                                                                         Caused by: java.io.NotSerializableException: android.graphics.drawable.BitmapDrawable
                                                                            at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1233)
                                                                            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:1817)
                                                                            at android.os.Parcel.writeValue(Parcel.java:1770) 
                                                                            at android.os.Parcel.writeList(Parcel.java:926) 
                                                                            at android.os.Parcel.writeValue(Parcel.java:1719) 
                                                                            at android.os.Parcel.writeArrayMapInternal(Parcel.java:838) 
                                                                            at android.os.BaseBundle.writeToParcelInner(BaseBundle.java:1542) 
                                                                            at android.os.Bundle.writeToParcel(Bundle.java:1232) 
                                                                            at android.os.Parcel.writeBundle(Parcel.java:878) 
                                                                            at android.support.v4.app.FragmentState.writeToParcel(Fragment.java:157) 
                                                                            at android.os.Parcel.writeTypedArray(Parcel.java:1462) 
                                                                            at android.support.v4.app.FragmentManagerState.writeToParcel(FragmentManager.java:617) 
                                                                            at android.os.Parcel.writeParcelable(Parcel.java:1791) 
                                                                            at android.os.Parcel.writeValue(Parcel.java:1697) 
                                                                            at android.os.Parcel.writeArrayMapInternal(Parcel.java:838) 
                                                                            at android.os.BaseBundle.writeToParcelInner(BaseBundle.java:1542) 
                                                                            at android.os.Bundle.writeToParcel(Bundle.java:1232) 
                                                                            at android.app.IActivityManager$Stub$Proxy.activityStopped(IActivityManager.java:4604) 
                                                                            at android.app.ActivityThread$StopInfo.run(ActivityThread.java:3934) 
                                                                            at android.os.Handler.handleCallback(Handler.java:790) 
                                                                            at android.os.Handler.dispatchMessage(Handler.java:99) 
                                                                            at android.os.Looper.loop(Looper.java:164) 
                                                                            at android.app.ActivityThread.main(ActivityThread.java:6494) 
                                                                            at java.lang.reflect.Method.invoke(Native Method) 
                                                                            at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438) 
                                                                            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807) 

EDIT:

/** * A {@link FragmentPagerAdapter} that returns a fragment corresponding to * one of the sections/tabs/pages. */ public class SectionsPagerAdapter extends FragmentPagerAdapter {

    public SectionsPagerAdapter(FragmentManager fm) {
        super(fm);
    }
    // Mudar o retorno do getItem de acordo com a position
    @Override
    public Fragment getItem(int position) {
        // getItem is called to instantiate the fragment for the given page.
         if(position == 0){
            return new CalendarFragment();
        }else if(position == 1){
            Bundle bundle = new Bundle();
            bundle.putSerializable("crops",crops);
            CropListFragment cropListFragment = new CropListFragment();
            cropListFragment.setArguments(bundle);
            return cropListFragment;
        }else if(position == 2){
            return new GeneralSearchFragment();
        }
        // Should'nt reach here
        return null;
    }

Crop example:

Crop abacaxi = new Crop("Harvest begins in 10th of June",
                getResources().getDrawable(R.drawable.abacax1),
                "Marvila",
                "Ananas comosus");

Crop object

public class Crop implements Serializable {

    private String specieHarvest;
    private Drawable specieImage;
    private String specieLocation;
    private String specieName;

    public Crop(String specieHarvest, Drawable specieImage, String specieLocation, String specieName) {
        this.specieHarvest = specieHarvest;
        this.specieImage = specieImage;
        this.specieLocation = specieLocation;
        this.specieName = specieName;
    }

    public String getSpecieHarvest() {
        return specieHarvest;
    }

    public Drawable getSpecieImage() {
        return specieImage;
    }

    public String getSpecieLocation() {
        return specieLocation;
    }

    public String getSpecieName() {
        return specieName;
    }
} 

回答1:

So I have an answer to the problem. This answers has base on the answers given on this post:

Why is it possible to pass a Drawable trough a Bundle from one Activity to a Fragment?

The answer is simple, as when passing objects as arguments to a Fragment will not cause it to be Marshled. This means it wouldn't be Serialized/Deserialized unless the Fragment is destroyed. And in this case he is destroyed when I try to start a new Activity. I tought Drawable was serializable cause I wasn't getting any errors while I was inside the Fragment, little did I know I was dealing with the same object (reference) itself.