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
Let’s assume that you want to pass an
ArrayList
of theSong
class from Activity1 to Activity2.1- The
Song
class should implement theSerializable
class.It would be something like that..
2-In
Activity1
pass your array list object as an extra toAtivity2
3- Finally receive the array list with
getSerializableExtra
inActivity2
You can create a singleton class for sharing your ArrayList across various components of android. Sample code for the singleton class is described below-
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.