将对象列表从Web服务发送到另一个活动

时间:2019-03-10 14:56:24

标签: java android android-intent

我想将对象列表从活动中的Web服务发送回另一个活动,但是我得到NullPointerException

我认为数组列表有问题,数据巫婆来自Web服务

    private void parsAndSendTheAudiDbAlbumDetail(String response) {

    try {
        Gson gson = new Gson();
        TheaudiodbAlbums theaudiodbAlbums = gson.fromJson(response, TheaudiodbAlbums.class);

        List<AlbumDetail> albumDetails = new ArrayList<>();

        for (int i = 0; i < theaudiodbAlbums.getAlbum().size(); i++) {

            AlbumDetail detail = new AlbumDetail();
            detail.setStrAlbum(theaudiodbAlbums.getAlbum().get(i).getStrAlbum());
            detail.setStrArtist(theaudiodbAlbums.getAlbum().get(i).getStrArtist());
            albumDetails.add(detail);
        }

        Bundle bundle = new Bundle();
        bundle.putSerializable("classList",(Serializable)albumDetails);
        returnIntent.putExtras(bundle);
        setResult(Activity.RESULT_OK, returnIntent);
        finish();
        }

这是第一个活动

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == 100 && resultCode == Activity.RESULT_OK) {


        //Set data to AlbumFragment

        Bundle bundle=getIntent().getExtras();

        List<AlbumDetail> albumDetailList= (List<AlbumDetail>) bundle.getSerializable("classList");

        Toast.makeText(this,albumDetailList.get(0).getStrAlbum(), Toast.LENGTH_SHORT).show();


    }

}

班级是:

public class AlbumDetail implements Serializable {
private String strAlbum;
private String strArtist;

public String getStrAlbum() {
    return strAlbum;
}

public void setStrAlbum(String strAlbum) {
    this.strAlbum = strAlbum;
}

public String getStrArtist() {
    return strArtist;
}

public void setStrArtist(String strArtist) {
    this.strArtist = strArtist;
}

}

1 个答案:

答案 0 :(得分:0)

使用getIntent();将提供用于启动活动的意图。要从活动结果中获取意图数据,请使用method参数中提供的意图。

Bundle bundle = data.getExtras();

代替

Bundle bundle = getIntent().getExtras();