Parcelable encountered IOException writing serializable object getactivity()

前端 未结 12 1808
故里飘歌
故里飘歌 2020-11-29 22:15

so I am getting this in logcat:

java.lang.RuntimeException: Parcelable encountered IOException writing serializable object (name = com.resources.student_list         


        
相关标签:
12条回答
  • 2020-11-29 22:25

    For me this was resolved by making the variable withing the class transient.

    Code before:

    public class UserLocation implements Serializable {
       public Location lastKnownLocation;
       public UserLocation() {}
    }
    

    code after

    public class UserLocation implements Serializable {
        public transient Location lastKnownLocation;
        public UserLocation() {}
    }   
    
    0 讨论(0)
  • 2020-11-29 22:32

    I faced Same issue, the issues was there are some inner classes with the static keyword.After removing the static keyword it started working and also the inner class should implements to Serializable

    Issue scenario

    class A implements Serializable{ 
      class static B{
      } 
    }
    

    Resolved By

    class A implements Serializable{ 
          class B implements Serializable{
          } 
        }
    
    0 讨论(0)
  • 2020-11-29 22:33

    I am also phase these error and i am little bit change in modelClass which are implemented Serializable interface like:

    At that Model class also implement Parcelable interface with writeToParcel() override method

    Then just got error to "create creator" so CREATOR is write and also create with modelclass contructor with arguments & without arguments..

           @Override
            public void writeToParcel(Parcel dest, int flags) {
                dest.writeString(id);
                dest.writeString(name);
            }
    
            protected ArtistTrackClass(Parcel in) {
                id = in.readString();
                name = in.readString();
            }
    
           public ArtistTrackClass() {
    
            }
    
        public static final Creator<ArtistTrackClass> CREATOR = new Creator<ArtistTrackClass>() {
            @Override
            public ArtistTrackClass createFromParcel(Parcel in) {
                return new ArtistTrackClass(in);
            }
    
            @Override
            public ArtistTrackClass[] newArray(int size) {
                return new ArtistTrackClass[size];
            }
        };
    

    Here,

    ArtistTrackClass -> ModelClass

    Constructor with Parcel arguments "read our attributes" and writeToParcel() is "write our attributes"

    0 讨论(0)
  • 2020-11-29 22:34

    The exception occurred due to the fact that any of the inner classes or other referenced classes didn't implement the serializable implementation. So make sure that all the referenced classes must implement the serializable implementation.

    0 讨论(0)
  • 2020-11-29 22:35

    In my case I had to implement MainActivity as Serializable too. Cause I needed to start a service from my MainActivity :

    public class MainActivity extends AppCompatActivity implements Serializable {
        ...
        musicCover = new MusicCover(); // A Serializable Object
        ...
        sIntent = new Intent(MainActivity.this, MusicPlayerService.class);
        sIntent.setAction(MusicPlayerService.ACTION_INITIALIZE_COVER);
        sIntent.putExtra(MusicPlayerService.EXTRA_COVER, musicCover);
        startService(sIntent);
    }
    
    0 讨论(0)
  • 2020-11-29 22:37

    if you POJO contains any other model inside that should also implements Serializable

    0 讨论(0)
提交回复
热议问题