Android:null在类中使用Parcelable接口时的nullPointerException与ArrayList <customobject> </customobject>

时间:2013-12-27 16:52:49

标签: android arraylist parcelable

我正在努力使名为Music的类使用parcelable,这样我就可以在两个不同的活动中访问音乐实例。我不想将serializable用于速度目的。当我尝试使用以下命令传递时,我不断收到nullPointerException:

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

Music Class :( 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,所以任何帮助/建议都非常受欢迎。提前谢谢!

2 个答案:

答案 0 :(得分:3)

将您的列表初始化为constructor.use以下代码。

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

并获取此代码:

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

并确保初始化thisInstance

答案 1 :(得分:0)

getParcelableArrayListExtra , getParcelableExtra 

以及

putParcelableArrayListExtra , putExtra(String name, Parcelable value)

用于通过意图传递Parcelable。