gpt4 book ai didi

Android:在带有 ArrayList 的类中使用 Parcelable 接口(interface)时出现 nullPointerException

转载 作者:行者123 更新时间:2023-11-29 17:54:07 26 4
gpt4 key购买 nike

我试图让名为 Music 的类使用 parcelable,这样我就可以在两个不同的 Activity 中访问 Music 的实例。我不想为了速度目的使用可序列化。当我尝试使用以下方式传递它时,我不断收到 nullPointerException:

Intent in = getIntent();
thisInstance = (Music) in.getExtras().get("MusicInstance");

音乐类:(ArrayList 在顶部,parcelable 类在底部)

public class Music implements Parcelable{

private static ArrayList<genericSongClass> songs = new ArrayList<genericSongClass>();

Cursor cursor;

static Context context;

public Music(Context context){
this.context = context;
}

public Music(Parcel in){
in.readTypedList(songs, genericSongClass.CREATOR);
}

public void BindAllSongs() {
/** Making custom drawable */
String selection = MediaStore.Audio.Media.IS_MUSIC + " != 0";
final String[] projection = new String[] {
MediaStore.Audio.Media.DISPLAY_NAME,
MediaStore.Audio.Media.ARTIST,
MediaStore.Audio.Media.DATA,
MediaStore.Audio.Media.ALBUM};
final String sortOrder = MediaStore.Audio.AudioColumns.TITLE
+ " COLLATE LOCALIZED ASC";

try {
// the uri of the table that we want to query
Uri uri = android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
// query the db
cursor = context.getContentResolver().query(uri,
projection, selection, null, sortOrder);
if (cursor != null) {
songs = new ArrayList<genericSongClass>(cursor.getCount());
cursor.moveToFirst();
while (!cursor.isAfterLast()) {
genericSongClass GSC = new genericSongClass();
GSC.songTitle = cursor.getString(0);
GSC.songArtist = cursor.getString(1);
GSC.songData = cursor.getString(2);
GSC.songAlbum = cursor.getString(3);
songs.add(GSC);
cursor.moveToNext();
}
}
} catch (Exception ex) {

} finally {
if (cursor != null) {
cursor.close();
}
}

}

public static Object[] toArray(ArrayList<Object> list){
Object[] toReturn = new Object[list.size()];
for (int i = 0; i < list.size(); i++){
toReturn[i] = list.get(i);
}
return toReturn;
}

public ArrayList<String> getArtists(){
ArrayList<String> artists = new ArrayList<String>();
for(genericSongClass gsc: songs){
if(!artists.contains(gsc.songArtist)){
artists.add(gsc.songArtist);
}
}
Alphabetize forArtists = new Alphabetize(artists);
return forArtists.getSortedArrayList();
}

public ArrayList<String> getAlbums(String artist){
ArrayList<String> albums = new ArrayList<String>();
for(genericSongClass gsc: songs){
if(gsc.songArtist == artist){
albums.add(gsc.songAlbum);
}
}
Alphabetize forAlbums = new Alphabetize(albums);
return forAlbums.getSortedArrayList();
}

//--- Parcel ------------------------------------------------

public static final Parcelable.Creator<Music> CREATOR = new Parcelable.Creator<Music>() {

public Music createFromParcel(Parcel in) {
return new Music(in);
}

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

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

@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeList(songs);
}



}

通用歌曲类:

public class genericSongClass implements Parcelable {
String songTitle = "";
String songArtist = "";
String songData = "";
String songAlbum = "";

public genericSongClass(){};

private genericSongClass(Parcel in){
songTitle = in.readString();
songArtist = in.readString();
songData = in.readString();
songAlbum = in.readString();
}

public static final Parcelable.Creator<genericSongClass> CREATOR = new Parcelable.Creator<genericSongClass>() {

@Override
public genericSongClass createFromParcel(Parcel source) {
return new genericSongClass(source);
}

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

};

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

@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(songTitle);
dest.writeString(songArtist);
dest.writeString(songData);
dest.writeString(songAlbum);
}
}

为什么我会得到空指针,我该如何补救?

这是我第一次使用 Parcelable,因此非常欢迎任何帮助/建议。提前致谢!

最佳答案

将您的列表初始化为构造函数。请使用以下代码。

public Music(Parcel in){
songs = new ArrayList<genericSongClass>();
in.readTypedList(songs, genericSongClass.CREATOR);
}

并获取此代码:

thisInstance = getIntent().getParcelableExtra("MusicInstance");

并确保初始化 thisInstance

关于Android:在带有 ArrayList<customObject> 的类中使用 Parcelable 接口(interface)时出现 nullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20804502/

26 4 0