当在类中的 Parcelable 接口使用 ArrayList < customObject > android 系统: nullPointerException

时间:2024-01-12 13:09:44

我想使调用音乐使用 parcelable,所以我可以访问两个不同的活动中的音乐的实例的类。我不想使用可序列化的速度的目的。当我尝试将它使用传递时,我总是收到 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);
} }

genericSongClass:

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);
}
}

为什么我得到 nullpointer 和如何作出补救这种情况吗?

这是我第一次使用 Parcelable,所以任何的帮助建议更是深受欢迎。先谢谢了!

解决方法 1:

到 constructor.use 请以下代码初始化您的列表。

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

和获得与此代码:

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

请确保您初始化和 thisInstance 。