When the Parcelable interface in the class uses ArrayList < CustomObject > Android system: NullPointerException

Source: Internet
Author: User

I want to make calls to music using parcelable, so I can access the class of two instances of music in different activities. I don't want to use the purpose of serializable speed. When I try to use it to pass, I always receive nullpointerexception:

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

Music classes: (ArrayList is at the bottom of the top and Parcelable Class)

public class Music implements parcelable{private static arraylist<genericsongclass> songs = new Arraylist<gen    Ericsongclass> ();    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 = MediaStor            E.audio.media.is_music + "! = 0"; Final string[] projection = new string[] {MediaStore.Audio.Media.DISPLAY_NAME, Medi AStore.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, we want to query             Uri uri = Android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI; Query the DB cursor = Context.getcontentresolver (). Query (URI, pro                        Jection, 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[] to        Return = 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& gt; () {@Override public genericsongclass createfromparcel (Parcel source) {return new Genericson        Gclass (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); }}

Why do I get nullpointer and how do I remedy this situation?

This is the first time I have used parcelable, so any help suggestions are more popular. Thank you, first!

Workaround 1:

To Constructor.use please initialize your list with the following code.

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

And get with this code:

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

Make sure that you initialize and thisInstance .

When the Parcelable interface in the class uses ArrayList < CustomObject > Android system: NullPointerException

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.