android

android-intent

android-activity

arraylist

parcelable

I am trying to pass an arraylist of objects between two activities, but my app crushes at the second activity. Can someone help me solve this problem...

Here is my code from my first activity:

Intent i = new Intent();
Bundle b = new Bundle();
b.putParcelableArrayList("songs",(ArrayList<? extends Parcelable>) albumsArray.get(position).getSongs());
Log.v("--", "OK");
i.putExtras(b);
i.setClass(LatestAlbums.this, AlbumDetails.class);
startActivity(i);

And code from the second activity:

songs=new ArrayList<Songs>();
Bundle b = this.getIntent().getExtras();
if(b!=null)
    songs = b.getParcelable("songs");
Log.v("--", songs.size()+"");

And my logcat output:

04-03 16:37:03.513: E/AndroidRuntime(6576): FATAL EXCEPTION: main
04-03 16:37:03.513: E/AndroidRuntime(6576): java.lang.RuntimeException: Parcel: unable to marshal value [email protected]419489e8
04-03 16:37:03.513: E/AndroidRuntime(6576):     at android.os.Parcel.writeValue(Parcel.java:1137)
04-03 16:37:03.513: E/AndroidRuntime(6576):     at android.os.Parcel.writeList(Parcel.java:524)
04-03 16:37:03.513: E/AndroidRuntime(6576):     at android.os.Parcel.writeValue(Parcel.java:1097)
04-03 16:37:03.513: E/AndroidRuntime(6576):     at android.os.Parcel.writeMapInternal(Parcel.java:493)
04-03 16:37:03.513: E/AndroidRuntime(6576):     at android.os.Bundle.writeToParcel(Bundle.java:1612)
04-03 16:37:03.513: E/AndroidRuntime(6576):     at android.os.Parcel.writeBundle(Parcel.java:507)
04-03 16:37:03.513: E/AndroidRuntime(6576):     at android.content.Intent.writeToParcel(Intent.java:6147)
04-03 16:37:03.513: E/AndroidRuntime(6576):     at android.app.ActivityManagerProxy.startActivity(ActivityManagerNative.java:1613)
04-03 16:37:03.513: E/AndroidRuntime(6576):     at android.app.Instrumentation.execStartActivity(Instrumentation.java:1379)
04-03 16:37:03.513: E/AndroidRuntime(6576):     at android.app.Activity.startActivityFromChild(Activity.java:3458)
04-03 16:37:03.513: E/AndroidRuntime(6576):     at android.app.Activity.startActivityForResult(Activity.java:3210)
04-03 16:37:03.513: E/AndroidRuntime(6576):     at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:817)
04-03 16:37:03.513: E/AndroidRuntime(6576):     at android.app.Activity.startActivity(Activity.java:3297)
04-03 16:37:03.513: E/AndroidRuntime(6576):     at com.outsourcefarm.musicagratis.activities.LatestAlbums$4$1.onItemClick(LatestAlbums.java:232)
04-03 16:37:03.513: E/AndroidRuntime(6576):     at android.widget.AdapterView.performItemClick(AdapterView.java:292)
04-03 16:37:03.513: E/AndroidRuntime(6576):     at android.widget.AbsListView.performItemClick(AbsListView.java:1058)
04-03 16:37:03.513: E/AndroidRuntime(6576):     at android.widget.AbsListView$PerformClick.run(AbsListView.java:2514)
04-03 16:37:03.513: E/AndroidRuntime(6576):     at android.widget.AbsListView$1.run(AbsListView.java:3168)
04-03 16:37:03.513: E/AndroidRuntime(6576):     at android.os.Handler.handleCallback(Handler.java:605)
04-03 16:37:03.513: E/AndroidRuntime(6576):     at android.os.Handler.dispatchMessage(Handler.java:92)
04-03 16:37:03.513: E/AndroidRuntime(6576):     at android.os.Looper.loop(Looper.java:137)
04-03 16:37:03.513: E/AndroidRuntime(6576):     at android.app.ActivityThread.main(ActivityThread.java:4440)
04-03 16:37:03.513: E/AndroidRuntime(6576):     at java.lang.reflect.Method.invokeNative(Native Method)
04-03 16:37:03.513: E/AndroidRuntime(6576):     at java.lang.reflect.Method.invoke(Method.java:511)
04-03 16:37:03.513: E/AndroidRuntime(6576):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:787)
04-03 16:37:03.513: E/AndroidRuntime(6576):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:554)
04-03 16:37:03.513: E/AndroidRuntime(6576):     at dalvik.system.NativeStart.main(Native Method)

Solution 1

You try the below

intent.putParcelableArrayListExtra("key", ArrayList<T extends Parcelable> list);
startActivity(intent);

Retrieve it

getIntent().getParcelableArrayListExtra("key");

Pass arraylist of user defined objects to Intent android. Check the answer by Sajmon

According to the comments made by Sajmon, Song class has to implement Parcelable

Solution 2

You should make your Song class implement Parcelable.

Solution 3

Intent intent=new Intent(ActivityFrom.this,ActivityTo.class);
intent.putExtra("StringKey",arrayList);
startActivity(intent);

and to retrieve

ArrayList<String> myList = (ArrayList<String>) getIntent().getSerializableExtra("StringKey");

Solution 4

     i.setClass(LatestAlbums.this, AlbumDetails.class);
     i.putStringArrayListExtra("list", your song list);
     startActivity(i);

To get the array list in activity.

ArrayList<String> fetchList= new ArrayList<String>();
fetchList=  getIntent().getStringArrayListExtra("list");

Hope this will help you.

For more reference use this link.

Solution 5

Refers to this question. I hope this example helps you.

1. Your Song class should be implements Parcelable Class

public class Song implements Parcelable { 
//Your setter and getter methods
}

2. Put your arraylist to putParcelableArrayListExtra()

public class ActivityA extends AppCompatActivity {
ArrayList<Song> songs;

@Override
protected void onCreate(Bundle savedInstanceState) {
button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            startActivity(new Intent(getApplicationContext(), ActivityB.class)
            .putParcelableArrayListExtra("songs", (ArrayList<? extends Parcelable>) songs));
        }
    });
}

3. In the ActivityB

public class ActivityB extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    Intent intent = getIntent();
    final ArrayList<Song> songs = intent.getParcelableArrayListExtra("songs");

    //Check the value in the console
    buttonCheck.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            for (Song value : songs) {
                System.out.println(value.getTitle());
            }
        }
    });
}

Solution 6

You can make the Songs class implement Serializable like this way.

public class Songs implements Serializable{ // your code here }.

In Activity One

Intent intent =  new Intent(MainActivity.this,Main2Activity.class);
Bundle bundle = new Bundle();
bundle.putSerializable("Songs",arrSongs); //where arrSongs is object of  ArrayList<Songs>

intent.putExtras(bundle);
startActivity(intent);

In SecondActivity where you want to retrieve the ArrayList object

 Bundle bundle =  getIntent().getExtras();
 private ArrayList<Songs> items;
 items = (ArrayList<Songs>)bundle.getSerializable("Songs");