我正在使用Parcelable作为我课堂上的自定义Song对象。但是,当我试图获得一个类型为Song的arraylist时,我总是得到一个空指针异常。我没有问题从意图获取字符串额外,但当我尝试获取此ArrayList时,我需要我总是得到null。当我试图传递一个Song对象时,我也得到一个null,所以我假设它有一些问题,但是我无法弄清楚它的生命。
这是我的歌曲课程
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];
}
};
}
这是我用来封装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);
这是检索PlayerService类
中的arraylistpublic 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
答案 0 :(得分:0)
您的参数错误
public static final String EXTRA_TRACK_LIST =“EXTRA_TRACK_LIST”;
这样改变
trackList = intent.getParcelableArrayListExtra(PlayerService.EXTRA_TRACK_LIST);
答案 1 :(得分:0)
队列是队列/列表还是类似?或者它是一个数组?如果是array,应该是queue.length,对吧?从列表到数组尝试URLConnection FTP list files。这应该有用。
intent.putParcelableArrayListExtra(PlayerService.EXTRA_TRACK_LIST, queue);
如果没有,请让我知道继续尝试。此外,您还可以将其作为String传递。您可以使用GSON将其转换为JSON字符串。转换为String后,将其作为单个String传递。
Intent intent = new Intent(ActivityA.this, Service.class);
intent.putExtra(new Gson().toJson(queue), PlayerService.EXTRA_TRACK_LIST);
context.startService(intent);
再次检索该对象:
Song song = new Gson().fromJson(intent.getStringExtra(PlayerService.EXTRA_TRACK_LIST), Song.class);
或者,如果列表(我相信你的情况):
Type listType = new TypeToken<ArrayList<Song.class>>(){}.getType();
List<Song> songList = new Gson().fromJson(intent.getStringExtra(PlayerService.EXTRA_TRACK_LIST), listType);