parcelable encountered ioexception writing serializable object…?

前端 未结 5 659
旧时难觅i
旧时难觅i 2021-01-19 01:43

The code

SngList.setOnItemClickListener(new OnItemClickListener() {
    public void onItemClick(AdapterView a, View v, int position, long id) {


        In         


        
相关标签:
5条回答
  • 2021-01-19 02:14

    In my case, there was a nested Delivery class inside the actual class Order (implements Serializable) which was not serialized, once Delivery class also implemented Serializable, the exception is gone. I didn't have to implement parcelable at all. Hope this helps someone!

    0 讨论(0)
  • 2021-01-19 02:20

    I know where the problem is in your case and that is about the BitMap All you need to do is Decode your BitMap before sending into intent

    Bitmap largeIcon = BitmapFactory.decodeResource(getResources(), R.drawable.thumbsup);
            ByteArrayOutputStream stream = new ByteArrayOutputStream();
            largeIcon.compress(Bitmap.CompressFormat.JPEG, 100, stream);
            byte[] byteArray = stream.toByteArray();
    

    And then send the following Object in intent

    intent.putExtras("remindermessage",object);
    

    and if not about the Bitmap then you should look for other things which might be taking more space and decode them before sending into intent

    0 讨论(0)
  • 2021-01-19 02:30

    Ok i implemented part of it for you. You have to add all the other properties of your SongDetails class:

    MainActivity.java:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
        SongDetails Songinfo1 = new SongDetails();
        Songinfo1.setSong("song1");
    
        SongDetails Songinfo2 = new SongDetails();
        Songinfo2.setSong("song2");
    
        ArrayList<SongDetails> list = new ArrayList<SongDetails>();
        list.add(Songinfo1);
        list.add(Songinfo2);
    
        Intent intent = new Intent(this, SecondActivity.class);
        intent.putParcelableArrayListExtra("Data1", list);
        intent.putExtra("Data2", 1);
        startActivity(intent);
    
    }
    

    In the activity in which you are retrieving the songs, use this:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_asdf);
    
        ArrayList<SongDetails> songs = getIntent().getParcelableArrayListExtra("Data1");
    
        for(SongDetails songDetails : songs) {
            Log.i("", songDetails.getSong());
        }
    }
    

    Your SongDetails class should look like this:

    SongDetails:

    public class SongDetails implements Parcelable {
        Bitmap icon;
        String song;
        String Artist;
        String Album;
        String Path;
        int icLauncher;
    
        public SongDetails() {
        }
    
        public SongDetails(Parcel in) {
            String[] data = new String[1];
            in.readStringArray(data);
            this.song = data[0];
        }
    
        public String getSong() {
            return song;
        }
    
        public void setSong(String song) {
            this.song = song;
        }
    
        public String getArtist() {
            return Artist;
        }
    
        public void setArtist(String Artist) {
            this.Artist = Artist;
        }
    
        public Bitmap getIcon() {
            return icon;
        }
    
        public void setIcon(Bitmap bitmap) {
            this.icon = bitmap;
        }
    
        public String getPath2() {
            return Path;
        }
    
        public void setPath2(String Path) {
            this.Path = Path;
        }
    
        public String getAlbum() {
            return Album;
        }
    
        public void setAlbum(String Album) {
            this.Album = Album;
        }
    
        public void setIcon(int icLauncher) {
            this.icLauncher = icLauncher;
        }
    
        @Override
        public int describeContents() {
            return 0;
        }
    
        @Override
        public void writeToParcel(Parcel dest, int flags) {
            dest.writeStringArray(new String[] { this.song });
        }
    
        public static final Parcelable.Creator CREATOR = new Parcelable.Creator() {
            public SongDetails createFromParcel(Parcel in) {
                return new SongDetails(in);
            }
    
            public SongDetails[] newArray(int size) {
                return new SongDetails[size];
            }
        };
    }
    
    0 讨论(0)
  • 2021-01-19 02:38

    According to https://forums.bignerdranch.com/t/challenge-saving-state-notserializableexception-error/8018/5 you should implement or extend some class from Parcelable. In my case I had a DialogFragment with an interface MyCallback extending Serializable. In newInstance(MyCallback callback) builder I used:

    Bundle args = new Bundle();
    args.putSerializable(key, callback);
    

    that led to an exception. Then I rewrote MyCallback to extend Parcelable and also added some methods to a callback when invoked this DialogFragment. At least, it doesn't crash on Home button or screen off.

    Also changed to:

    Bundle args = new Bundle();
    args.putParcelable(key, callback);
    
    0 讨论(0)
  • 2021-01-19 02:39

    To get the arraylist from another activity.

          ArrayList<SongDetails> list = new ArrayList<SongDetails>();
                  list=  getIntent().getStringArrayListExtra("Data1");
    

    To pass the arraylist to another activity.

    intent.putStringArrayListExtra("Data1", Songinfo);
    

    To split the array list:

            for(SongDetails name: list)
            {
            String yoursong= name.song;
            }
    

    Hope this will give you some solution.

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