在活动之间传递对象的arraylist

我试图在两个活动之间传递一个对象的arraylist,但我的应用程序在第二个活动中粉碎。 有人可以帮我解决这个问题……

这是我第一次活动的代码:

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

和第二个活动的代码:

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

我的logcat输出:

 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 com.musicapp.objects.Songs@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) 

  • Unmarshalling Android Intent Parcelable时找不到类
  • 如何使界面Parcelable?
  • 把一个对象放在Handler消息中
  • Android中使用的Parcelable和Serialization有什么区别?
  • 传递ArrayList <? 实现Parcelable>到Activity
  • 如何修复Android中偏移YYY的Unmarshalling未知types代码XXX?
  • 抽象类作为parcelable
  • 嵌套的parcelable对象不读
  • 你应该让你的Song类实现Parcelable

    你试试下面的

     intent.putParcelableArrayListExtra("key", ArrayList list); startActivity(intent); 

    检索它

     getIntent().getParcelableArrayListExtra("key"); 

    将用户定义对象的arraylist传递给Intent android 。 检查Sajmon的答案

    根据Sajmon的评论, Song类必须implement Parcelable

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

    并检索

     ArrayList myList = (ArrayList) getIntent().getSerializableExtra("StringKey"); 
      i.setClass(LatestAlbums.this, AlbumDetails.class); i.putStringArrayListExtra("list", your song list); startActivity(i); 

    获取活动中的数组列表。

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

    希望这会帮助你。

    有关更多参考,请使用此链接 。