Android - 在活动之间传递对象erorr

时间:2016-04-17 12:40:14

标签: android android-activity

歌手对象:

public class Singer  implements java.io.Serializable {

    public String  id ;
    public String name;
    public HashMap<String,String> songs;

    public Singer(String name,String id){
        this.id=id;
        this.name=name;
        this.songs=new HashMap<>();

    }

    public void AddingSong(String songName,String songURL){
        this.songs.put(songName,songURL);
    }
}

MainClass:

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                //starting the mediaPlayer Activity with the song url
                Intent intent = new Intent(getActivity(), songs_page.class);

                intent.putExtra("name",(String)listView.getAdapter().getItem(position));
                intent.putExtra("id", songs.get(listView.getAdapter().getItem(position)));

                Singer Obj=new Singer("1","Bayan");
                Obj.AddingSong("koko","[song URL here]");
                intent.putExtra("Allsongs",Obj);


                startActivityForResult(intent, 0);
            }
        });

歌曲活动:

歌手歌手=(歌手)getIntent()。getSerializable(“Allsongs”);

将活动中的字符串发送到另一个活动是有效的,但是当我想传递对象时,它就开始给我一个错误。

1 个答案:

答案 0 :(得分:2)

使用捆绑包:

Bundle data = new Bundle();
data.putSerializable("Allsongs", yourObject);

intent.putExtras(data);

然后在另一项活动中:

Intent intent = getIntent();
Bundle data = intent.getExtras();
Singer singer =(Singer) data.getSerializable("Allsongs");