skip to Main Content

I am making a mp3 player app , in my main activity I am showing the list of all songs in recycler view and when user click on the song I am trying to send entire array list of songs to my player activity , where I can work for with next and previous songs play , but my app crashes when click the song

  Process: com.choudhary.musicplayer, PID: 8686
java.lang.RuntimeException: Parcel: unable to marshal value com.choudhary.musicplayer.AudioModel@a0de380
    at android.os.Parcel.writeValue(Parcel.java:1667)
    at android.os.Parcel.writeList(Parcel.java:966)
    at android.os.Parcel.writeValue(Parcel.java:1614)
    at android.os.Parcel.writeArrayMapInternal(Parcel.java:878)
    at android.os.BaseBundle.writeToParcelInner(BaseBundle.java:1588)
    at android.os.Bundle.writeToParcel(Bundle.java:1233)
    at android.os.Parcel.writeBundle(Parcel.java:918)
    at android.content.Intent.writeToParcel(Intent.java:9987)
    at android.app.IActivityManager$Stub$Proxy.startActivity(IActivityManager.java:3636)
    at android.app.Instrumentation.execStartActivity(Instrumentation.java:1675)
    at android.app.Activity.startActivityForResult(Activity.java:4651)
    at androidx.activity.ComponentActivity.startActivityForResult(ComponentActivity.java:597)
    at android.app.Activity.startActivityForResult(Activity.java:4609)
    at androidx.activity.ComponentActivity.startActivityForResult(ComponentActivity.java:583)
    at android.app.Activity.startActivity(Activity.java:4970)
    at android.app.Activity.startActivity(Activity.java:4938)
    at com.choudhary.musicplayer.MusicAdapter$1.onClick(MusicAdapter.java:54)
    at android.view.View.performClick(View.java:6608)
    at android.view.View.performClickInternal(View.java:6585)
    at android.view.View.access$3100(View.java:785)
    at android.view.View$PerformClick.run(View.java:25921)

My Adapter’s OnBind method :–

@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {


    holder.name.setText(arrayList.get(position).getaName());
    holder.album.setText(arrayList.get(position).getaAlbum());

  holder.imageView.setImageBitmap(BitmapFactory.decodeFile(arrayList.get(position).getAlbumart()));

    holder.imageView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent in = new Intent(context,PlayerActivity.class);
            in.putExtra("SONG",arrayList.get(position).getaName());
            in.putExtra("PATH",arrayList.get(position).getaPath());
            in.putExtra("ALBUM",arrayList.get(position).getaAlbum());
            in.putExtra("LIST",arrayList);
            in.putExtra("POSITION",  arrayList.get(position).toString());

            context.startActivity(in);
        }
    });


}

my Player Activity :—

public class PlayerActivity extends AppCompatActivity {

TextView songanme, songAlbum,duration,movetime;
ImageView playbutton,nextbtn,previousbtn;
SeekBar seekBar;
MediaPlayer mediaPlayer ;

ArrayList<AudioModel>  list;

int CURRENT_POSITION ;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_player);

    songanme = findViewById(R.id.music_name_pl);
    movetime = findViewById(R.id.move_time);
    seekBar = findViewById(R.id.seekBar);
    songAlbum = findViewById(R.id.music_album_pl);
    duration = findViewById(R.id.duration);

    playbutton = findViewById(R.id.play_btn_pl);
    nextbtn = findViewById(R.id.next_btn_pl);
    previousbtn = findViewById(R.id.previous_pl);

    list = new ArrayList<>();

    songanme.setSelected(true);


    Intent intent = getIntent();
    Bundle bundle = intent.getExtras();

    list =    (ArrayList) bundle.getParcelableArrayList("LIST");

}

2

Answers


  1. Let’s assume that you want to pass an ArrayList of the Song class from Activity1 to Activity2.

    1- The Song class should implement the Serializable class.

    It would be something like that..

    import java.io.Serializable;
    
    public class Song implements Serializable {
        String name;
        String album;
    
        public Song(String name, String album) {
            this.name = name;
            this.album = album;
        }
    }
    

    2-In Activity1 pass your array list object as an extra to Ativity2

            ArrayList<Song> songs= new ArrayList();
            songs.add(new Song("song1","album1"));
            songs.add(new Song("song2","album2"));
            songs.add(new Song("song3","album3"));
    
            Intent intent=new Intent(this,Activity2.class);
            intent.putExtra("songs",songs);
            startActivity(intent);
    

    3- Finally receive the array list with getSerializableExtra in Activity2

    ArrayList<Song> songs = (ArrayList<Song>) getIntent().getSerializableExtra("songs");
    Log.i("HINT", "" + songs.size());
    
    Login or Signup to reply.
  2. You can create a singleton class for sharing your ArrayList across various components of android. Sample code for the singleton class is described below-

    public class SongBank
    {
    
        private static SongBank instance;
    
        private ArrayList<Song> songsArrayList;
    
        private SongBank(Context context)
            {
            // You can do any stuff if you want here      
            }
    
    
    // create getter and setter methods for your arrayList
    
        public void setSongsList(ArrayList<Song> songs)
        {
            if(songs!=null)
            {
                this.songsArrayList=songs; 
            }
        }
    
        public ArrayList<Song> getSongsList()
        {
            return this.songsArrayList;
        }
    
    
    
        public static SongBank getInstance(Context context)
        {
            if(instance==null)
            {
                instance=new SongBank(context);
            }  
    
            return instance;
    
        }
    
    
    
    }
    

    The instance of this singleton class can be called across various activities or fragments and you can also change the Arraylist value across different activities if you want. You can also call this class and get the list in your music service without worrying about serialization.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search