J'utilise Parcelable pour un objet Song personnalisé que j'ai dans ma classe. Cependant, quand j'essaie d'obtenir une liste supplémentaire de type Song, je reçois toujours une exception de pointeur nul. Je n'ai aucun problème à obtenir des extras de chaîne à partir de l'intention mais quand j'essaye d'obtenir cette ArrayList j'ai besoin de toujours obtenir la valeur null. Je reçois également une valeur nulle lorsque j'essaie juste de passer un objet Song, donc je suppose qu'il y a un problème avec ça mais je ne peux pas le comprendre pour la vie de moi.getParcelableArrayExtra null pointeur
C'est ma classe de chanson
import android.os.Parcel;
import android.os.Parcelable;
public class Song implements Parcelable {
private String uri;
private String title;
private String artist;
private String album;
private String length;
private int count;
private int source;
public Song() {
}
public Song (String title, String artist, String album, String uri, String length, int count,
int source) {
this.uri = uri;
this.title = title;
this.artist = artist;
this.album = album;
this.length = length;
this.count = count;
this.source = source;
}
public String getUri() {
return uri;
}
public String getArtist() {
return artist;
}
public String getTitle() {
return title;
}
public String getLength() {
return length;
}
public int getCount() {return count;}
@Override
public String toString() {
return title + " - " +artist;
}
@Override
public boolean equals(Object o){
if (o instanceof Song) {
Song song = (Song) o;
if (title.equals(song.title) && length.equals(song.length)) {
return true;
}
}
return false;
}
public int compareTo(Song s) {
if (this.count < s.getCount()) {
return -1;
}
else if(this.count > s.getCount()){
return 1;
}
return 0;
}
public String getAlbum() {
return album;
}
public void setAlbum(String album) {
this.album = album;
}
public int getSource() {
return source;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(this.uri);
dest.writeString(this.title);
dest.writeString(this.artist);
dest.writeString(this.album);
dest.writeString(this.length);
dest.writeInt(this.count);
dest.writeInt(this.source);
}
protected Song(Parcel in) {
this.uri = in.readString();
this.title = in.readString();
this.artist = in.readString();
this.album = in.readString();
this.length = in.readString();
this.count = in.readInt();
this.source = in.readInt();
}
public static final Creator<Song> CREATOR = new Creator<Song>() {
@Override
public Song createFromParcel(Parcel source) {
return new Song(source);
}
@Override
public Song[] newArray(int size) {
return new Song[size];
}
};
}
Ceci est la ligne que j'utilise pour emballer le arraylist
Intent intent = new Intent();
Log.d("UTILS ", " size: " +queue.size()); // Making sure it is not null before passing
intent.setClass(context, PlayerService.class);
intent.putParcelableArrayListExtra(PlayerService.EXTRA_TRACK_LIST, queue);
context.startService(intent);
Ce récupère la arraylist en classe PlayerService
public static final String EXTRA_TRACK_LIST = "EXTRA_TRACK_LIST";
private ArrayList<Song> trackList;
.
.
.
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (intent == null || intent.getAction() == null) {
Log.d(TAG, "unspecified command");
return START_STICKY;
}
trackList=intent.getParcelableArrayListExtra(PlayerService.EXTRA_TRACK_LIST);
if (trackList == null) {
Log.d("TRACKLIST", "IS NULL");
} else {
Log.d(TAG, "size: "+trackList.size());
}
.
.
.
// more irrelevant code
vous passez d'une activité au service? –
@SohailZahid en quelque sorte. Je passe d'une vue personnalisée qui appelle la méthode via un fragment pour passer l'arraylist au service. – osharifali
votre service étendent 'Intent service' ou juste' service'? –