2

I have the class Playlist

public class Playlist extends ArrayList<Track> implements Serializable {

    String module;
    String name, playlistId;

    public Playlist(String name) {
        super();
        this.name = name;
    }
    ...
}

and I try to give it to a Intent with:

...
Intent intent = new Intent(this, PlaylistActivity.class);
intent.putExtra("playlist", Modul.loaded[id].getPlaylist(pId)); //Return Playlist
startActivity(intent);

And when I try to get the Playlist with:

Intent intent = getIntent();
playlist = (Playlist) intent.getSerializableExtra("playlist");

And here I get this error:

Caused by: java.lang.ClassCastException: java.util.ArrayList cannot be cast to com.timia2109.nukla.Playlist

But it must be a Playlist, because I create it as a Playlist and I don't will cast it to a Serializable with the Intent. I don't find a solution on the Internet, but I don't found anything.

Or is there a way to do that:

PlaylistActivity pa = new PlaylistActivity();
pa.setPlaylist( playlist );
//And now start this Activity

Thanks!

timia2109
  • 732
  • 1
  • 5
  • 11

1 Answers1

1

There was an old bug related to this that is now marked as obsolete. I don't know if it is still an issue. The simplest solution though is of course to manually handle the (re)creation of your object

Ideally start by changing your implementation to Parcelable something like the following which is similar to something I've used in the past

public static class Playlist extends ArrayList<Track> implements Parcelable {

    String module;
    String name, playlistId;

    public Playlist(String name) {
        super();
        this.name = name;
    }

    protected Playlist(Parcel in) {
        module = in.readString();
        name = in.readString();
        playlistId = in.readString();
        //this line you might need to tweak & error handle, I've jsut written this from memory
        super.addAll( (Collection<? extends Track>) Arrays.asList( in.readParcelableArray(Track.class.getClassLoader() )) );
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(module);
        dest.writeString(name);
        dest.writeString(playlistId);
        dest.writeParcelableArray(toArray(new Track[size()]), flags);
    }

    public static final Creator<Playlist> CREATOR = new Creator<Playlist>() {
        @Override
        public Playlist createFromParcel(Parcel in) {
            return new Playlist(in);
        }

        @Override
        public Playlist[] newArray(int size) {
            return new Playlist[size];
        }
    };

}

The reasons for preferring parcelable are explained well here

Your receiving activity should handle a parcelable better. Whether you decide to make your object Parcelable or not you can handle the recreation yourself like follows

public static class Playlist ... {

    public Playlist(ArrayList<Track> arrList) {
        super( arrList );
    }

    ....
}

and using

Intent intent = getIntent();
ArrayList<Track> arrList = (ArrayList<Track>) intent.getSerializableExtra("playlist");
playlist = new Playlist(arrList);

You'll just need to modify it for those extra two fields you have and be aware that when you try to write it to an intent it might complain about generics, so simply manually cast to parcelable/serializable when you set the extra:

intent.putExtra("playlist", (Parcelable) Modul.loaded[id].getPlaylist(pId));
Community
  • 1
  • 1
Nick Cardoso
  • 20,807
  • 14
  • 73
  • 124
  • Thanks! That works :) But I only get it so: protected Playlist(Parcel in) { module = in.readString(); name = in.readString(); playlistId = in.readString(); //this line you might need to tweak & error handle, I've jsut written this from memory List load = Arrays.asList( in.readParcelableArray(Track.class.getClassLoader() ) ); for (int i=0; i – timia2109 Dec 27 '15 at 13:18