18

我试图在两个活动之间传递一个对象数组列表,但我的应用程序在第二个活动中崩溃了。谁能帮我解决这个问题...

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

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);

第二个活动的代码:

songs=new ArrayList<Songs>();
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)
4

6 回答 6

22

你试试下面

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

取回它

getIntent().getParcelableArrayListExtra("key");

将用户定义对象的数组列表传递给 Intent android。检查 Sajmon 的答案

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

于 2013-04-03T14:51:02.637 回答
12

你应该让你的Song班级实现Parcelable

于 2013-04-03T15:49:00.850 回答
8
Intent intent=new Intent(ActivityFrom.this,ActivityTo.class);
intent.putExtra("StringKey",arrayList);
startActivity(intent);

并检索

ArrayList<String> myList = (ArrayList<String>) getIntent().getSerializableExtra("StringKey");
于 2016-11-21T09:18:11.990 回答
3
     i.setClass(LatestAlbums.this, AlbumDetails.class);
     i.putStringArrayListExtra("list", your song list);
     startActivity(i);

获取活动中的数组列表。

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

希望这会帮助你。

如需更多参考,请使用此链接

于 2013-04-03T14:47:44.040 回答
2

这个问题。我希望这个例子对你有所帮助。

1. 你的 Song 类应该是实现 Parcelable 类

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

2. 将你的数组列表放入 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.在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());
            }
        }
    });
}
于 2019-02-16T09:45:02.900 回答
0

您可以像这样使 Songs 类实现 Serializable 。

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

在活动一中

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);

在您要检索 ArrayList 对象的 SecondActivity 中

 Bundle bundle =  getIntent().getExtras();
 private ArrayList<Songs> items;
 items = (ArrayList<Songs>)bundle.getSerializable("Songs");
于 2020-04-14T23:39:07.203 回答