我正在开发一个媒体播放器应用程序,其中使用 ArrayList
存储歌曲列表,并希望在 Service
和其他 Activities
之间使用相同的列表。 我编写了一个实现 Parcelable
接口的自定义类型 Songs。下面是我的做法:
String ID, Title, Artist, Album, Genre, Duration, Path;
byte[] AlbumArt;
//constructors go here
//getters and setters go here
public Songs(Parcel in) {
readFromParcel(in);
}
@Override
public void writeToParcel(Parcel dest, int flags) {
// TODO Auto-generated method stub
dest.writeString(this.ID);
dest.writeString(this.Title);
dest.writeString(this.Artist);
dest.writeString(this.Album);
dest.writeString(this.Genre);
dest.writeString(this.Duration);
dest.writeByteArray(this.AlbumArt);
dest.writeString(this.Path);
}
private void readFromParcel(Parcel in) {
this.ID = in.readString();
this.Title = in.readString();
this.Artist = in.readString();
this.Album = in.readString();
this.Genre = in.readString();
this.Duration = in.readString();
in.readByteArray(this.AlbumArt);
this.Path = in.readString();
}
public static final Parcelable.Creator<Songs> CREATOR = new Parcelable.Creator<Songs>() {
@Override
public Songs createFromParcel(Parcel source) {
// TODO Auto-generated method stub
return new Songs(source); // using parcelable constructor
}
@Override
public Songs[] newArray(int size) {
// TODO Auto-generated method stub
return new Songs[size];
}
};
现在的问题是,当我试图通过Intent传递ArrayList时,我会收到“FAILED BINDER TRANSACTION”的错误消息。作为解决方法,我正在使用静态变量。有没有什么办法可以克服这个问题,并将ArrayList传递给Intent?