创建对象并将其传递给活动失败
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了创建对象并将其传递给活动失败相关的知识,希望对你有一定的参考价值。
我想通过intent创建对象并将其传递给另一个活动,因此我可以处理此活动中的对象详细信息并将其显示给用户。
该对象由值long标识。我有一个arraylist(releaseIds),它保存了这个对象的几个long值,所以我只想将这些值通过intent传递给另一个activity。
用户可以从呈现该列表的警报对话框中选择所需对象,一旦他/她从该列表中选择了一个项目,就识别出正确的长ID并创建一个新对象并通过意图传递。
但是创建新对象会以某种方式失败,因为对象ReleaseModel在接收活动中为空。
我做错了什么?
创建并传递对象(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);
...
}
}
基础模型:
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]", "");
}
}
答案
将ReleaseModel对象传递给另一个Activity(ActivityPassedTo)。执行以下步骤:
1.您需要使ReleaseModel实现Serializable
2.要将其传递给Intent,您需要使用唯一键来标识在两个活动中传递的对象
例如。你可以创建一个密钥:static final String RELEASE_MODEL = "RELEASE_MODEL";
- 现在将其传递给意图中的活动
Intent intent = new Intent(this,ActivityPassedTo.class);
intent.putExtra(RELEASE_MODEL,releaseModelObject);
startActivity(intent);
- 现在检索Activity中的对象(ActivityPassedTo)
Intent intent = getIntent()
ReleaseModel releaseModel = (ReleaseModel)intent.getSerializableExtra(RELEASE_MODEL);
以上是关于创建对象并将其传递给活动失败的主要内容,如果未能解决你的问题,请参考以下文章