创建对象并将其传递给活动失败

时间:2017-12-26 08:28:56

标签: android object android-intent annotations parcelable

我想通过意图创建对象并将其传递给另一个活动,因此我可以处理此活动中的对象详细信息并将其显示给用户。

此对象由值long标识。 我有一个arraylist(releaseIds),它保存了这个对象的几个long值,所以我只想将这些值通过intent传递给另一个activity。

用户可以从显示此列表的alertdialog中选择所需的对象,一旦他/她从此列表中选择了一个项目,就会识别出正确的长ID并创建一个新对象并通过意图传递。

但是创建新对象会以某种方式失败,因为接收活动中的对象ReleaseModel为null

我做错了什么?

创建并传递对象(ReleaseModel):

...
new DialogInterface.OnClickListener() {

    public void onClick(DialogInterface dialog, int item) {
    // Do something with the selection
    for (int i = 0; i < releaseIds.size(); i++) {

    if (i == item) {
       long ID = releaseIds.get(i);
       final ReleaseModel releaseModel = new ReleaseModel(ID);
       ReleaseActivtiy_.intent(MyAppsMain.this).extra("releaseModel",
           releaseModel).start();
        }
       }
      }
});
...

对象本身:

public class ReleaseModel extends BaseModel {

    private List<String> url = new ArrayList<>();
    private boolean liked;
    ...

    public ReleaseModel() {
    }

    public ReleaseModel(long id) {
        super(id);
    }
    ...
    public void writeToParcel(Parcel parcel, int flags) {
        super.writeToParcel(parcel, flags);
        parcel.writeInt(liked ? 1 : 0);
        ...
    }

    public static final Parcelable.Creator<ReleaseModel> CREATOR = new Parcelable.Creator<ReleaseModel>() {
        public ReleaseModel createFromParcel(Parcel in) {
            return new ReleaseModel(in);
        }

        public ReleaseModel[] newArray(int size) {
            return new ReleaseModel[size];
        }
    };

    private ReleaseModel(Parcel parcel) {
        super(parcel);
        setLiked(parcel.readInt() == 1);
        ...
    }
}

basemodel:

public class BaseModel implements Parcelable {

    private long id;
    private long date;
    ...
    public BaseModel() {
    }

    public BaseModel(long id) {
        this.id = id;
    }

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }
    ...
    public void writeToParcel(Parcel parcel, int flags) {
        parcel.writeLong(id);
        parcel.writeLong(date);
        ...
    }

    public static final Parcelable.Creator<BaseModel> CREATOR = new Parcelable.Creator<BaseModel>() {
        public BaseModel createFromParcel(Parcel in) {
            return new BaseModel(in);
        }

        public BaseModel[] newArray(int size) {
            return new BaseModel[size];
        }
    };

    public BaseModel(Parcel parcel) {
        setId(parcel.readLong());
        setDate(parcel.readLong());
        ...
    }
}

接收活动:

@EActivity(R.layout.release_activity)
public class ReleaseActivtiy extends BaseActivity implements LoaderManager
        .LoaderCallbacks<BaseResponse>, NotificationCenter.NotificationCenterDelegate {
...
@Extra("releaseModel")
    ReleaseModel releaseModel;

    @Override
        public void onCreate(Bundle savedInstanceState) {

            super.onCreate(savedInstanceState);
            ...
        }

        @AfterViews
        public void init() {
            // Nullpointer exception on this line for releaseModel
            String temp = releaseModel.getSubject().replaceAll("[^a-zA-Z]", "");
        }
}

1 个答案:

答案 0 :(得分:0)

将ReleaseModel对象传递给另一个Activity(ActivityPassedTo)。 执行以下步骤:

1.您需要使ReleaseModel实现Serializable

2.要将其传递给Intent,您需要使用唯一键来标识在两个活动中传递的对象

EG。你可以创建一个密钥: static final String RELEASE_MODEL = "RELEASE_MODEL";

  1. 现在将其传递给意图中的活动

    Intent intent = new Intent(this,ActivityPassedTo.class);

    intent.putExtra(RELEASE_MODEL,releaseModelObject);

    startActivity(intent);

  2. 现在在Activity(ActivityPassedTo)中检索对象

    Intent intent = getIntent()

    ReleaseModel releaseModel = (ReleaseModel)intent.getSerializableExtra(RELEASE_MODEL);