gpt4 book ai didi

java - getParcelableArrayExtra 空指针

转载 作者:太空宇宙 更新时间:2023-11-04 12:18:43 26 4
gpt4 key购买 nike

我正在将 Parcelable 用于类中的自定义 Song 对象。但是,当我尝试获取 Song 类型的额外数组列表时,我总是会遇到空指针异常。我从 Intent 中获取字符串额外内容没有问题,但是当我尝试获取这个 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];
}
};
}

这是我用来打包数组列表的行

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 类中的数组列表

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

最佳答案

你的参数错误

公共(public)静态最终字符串EXTRA_TRACK_LIST =“EXTRA_TRACK_LIST”;

所以像这样改变

trackList = intent.getParcelableArrayListExtra(PlayerService.EXTRA_TRACK_LIST);

关于java - getParcelableArrayExtra 空指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39092746/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com