我试图将额外内容从一个活动传递到另一个活动。我还有一个名为Objects的类,它包含附加内容的对象。
在我的第一次活动中,我写了这个:
private OnClickListener top_listener = new OnClickListener() {
public void onClick(View v) {
Intent top = new Intent(Main.this, TopProject.class);
Bundle b = new Bundle();
b.putParcelable("project_title", Objects);
startActivity(top);
}
};
但是这行有一个语法错误:b.putParcelable(“project_title”,Objects);它说“对象”无法解析为变量。我应该先申报其他内容还是什么?谢谢
这是我的对象类:
public class Objects implements Parcelable{
String project_title = "project title";
String organization_title = "organization title";
String keyword = "keyword";
String short_code = "short code";
String project_description = "description";
String smallImageUrl = "smallImageUrl";
String bigImageUrl = "bigImageUrl";
String price= "price";
String country= "country";
public static ArrayList<Project> projectsList = new ArrayList<Project>();
public Objects(String project_title, String organization_title, String keyword, String short_code, String project_description, String smallImageUrl, String bigImageUrl, String price, String country) {
this.project_title = project_title;
this.organization_title = organization_title;
this.keyword = keyword;
this.short_code = short_code;
this.project_description = project_description;
this.smallImageUrl = smallImageUrl;
this.bigImageUrl = bigImageUrl;
this.price = price;
this.country = country;
}
private Objects(Parcel parcel) {
project_title = parcel.readString();
organization_title = parcel.readString();
keyword = parcel.readString();
short_code = parcel.readString();
project_description = parcel.readString();
smallImageUrl = parcel.readString();
bigImageUrl = parcel.readString();
price = parcel.readString();
country = parcel.readString();
//projectsList = parcel.readList();
}
public int describeContents() {
return 0;
}
public void writeToParcel(Parcel parcel, int flags) {
parcel.writeString(project_title);
parcel.writeString(organization_title);
parcel.writeString(keyword);
parcel.writeString(short_code);
parcel.writeString(project_description);
parcel.writeString(smallImageUrl);
parcel.writeString(bigImageUrl);
parcel.writeString(price);
parcel.writeString(country);
parcel.writeList(projectsList);
}
public String getProjectTitle(){
return project_title;
}
public String getOrganizationTitle(){
return organization_title;
}
public String getProjectDescription(){
return project_description;
}
public String getKeyword(){
return keyword;
}
public String getShortcode(){
return short_code;
}
public String getSmallImageUrl(){
return smallImageUrl;
}
public String getBigImageUrl(){
return bigImageUrl;
}
public String getPrice(){
return price;
}
public String getCountry(){
return country;
}
public void setProjectTitle(String project_title){
this.project_title = project_title;
}
public void setOrganizationTitle(String organization_title){
this.organization_title = organization_title;
}
public void setProjectDescription(String project_description){
this.project_description = project_description;
}
public void setKeyword(String keyword){
this.keyword = keyword;
}
public void setShortcode(String short_code){
this.short_code = short_code;
}
public void setSmallImageUrl(String smallImageUrl){
this.smallImageUrl = smallImageUrl;
}
public void setBigImageUrl(String bigImageUrl){
this.bigImageUrl = bigImageUrl;
}
public void setPrice(String price){
this.price = price;
}
public void setCountry(String country){
this.country = country;
}
public static Creator<Objects> CREATOR
= new Creator<Objects>() {
public Objects createFromParcel(Parcel parcel) {
return new Objects(parcel);
}
public Objects[] newArray(int size) {
return new Objects[size];
}
};
答案 0 :(得分:1)
b.putParcelable("project_title", Objects);
这意味着什么都没有。你必须提供你班级的实例。类似的东西:
b.putParcelable("project_title", new Objects());
答案 1 :(得分:1)
如果它只是一个带有基元和Serializables的简单类,请使用Serializable,而不是Parcelable。它会让你的生活更轻松。传递的Objects对象也必须是一个实例,如其他答案中所述
e.g。
Objects object = new Objects(); //assuming Objects implements Serializable
bundle.putSerializable("blahblah", object);
答案 2 :(得分:0)
你不能将“Oject”作为争论传递,因为它定义了类的名称。在那里提供该类的引用。