Android getParcelableExtra对象始终返回null
所以我试图将一个对象作为可包裹的从一个活动发送到另一个活动,但是接收方的对象总是空的。对象在发送方完全被填充,所以我几乎可以肯定它与读取对象有关 这是可包裹的对象:Android getParcelableExtra对象始终返回null,android,android-intent,null,parcelable,extra,Android,Android Intent,Null,Parcelable,Extra,所以我试图将一个对象作为可包裹的从一个活动发送到另一个活动,但是接收方的对象总是空的。对象在发送方完全被填充,所以我几乎可以肯定它与读取对象有关 这是可包裹的对象: import android.graphics.Bitmap; import android.os.Parcel; import android.os.Parcelable; public class Picture implements Parcelable { public String pictureID, tit
import android.graphics.Bitmap;
import android.os.Parcel;
import android.os.Parcelable;
public class Picture implements Parcelable {
public String pictureID, title, year, price, author;
public Bitmap picture;
public Picture(){
}
public int describeContents() {
return 0;
}
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(pictureID);
dest.writeString(title);
dest.writeString(year);
dest.writeString(price);
dest.writeString(author);
dest.writeParcelable(picture, flags);
}
protected Picture(Parcel in) {
pictureID = in.readString();
title = in.readString();
year = in.readString();
price = in.readString();
author = in.readString();
picture = in.readParcelable(Bitmap.class.getClassLoader());
}
public static final Parcelable.Creator<Picture> CREATOR = new Parcelable.Creator<Picture>() {
public Picture createFromParcel(Parcel source) {
return new Picture(source);
}
public Picture[] newArray(int size) {
return new Picture[size];
}
};
}
Picture firstPic = new Picture();
firstPic.pictureID = "1";
firstPic.title = "Mona Lisa";
firstPic.author = "Leonardo Da Vinci";
firstPic.price = "99999000";
firstPic.year = "2250";
firstPic.picture = BitmapFactory.decodeResource(getApplication().getResources(),R.drawable.monalisa);
Intent i = new Intent(getApplicationContext(),MainScreen.class);
i.putExtra("first",firstPic);
startActivity(i);
finish();
Picture currentPicture = new Picture();
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getIntent();
currentPicture = intent.getParcelableExtra("first");
}
接收者活动:
import android.graphics.Bitmap;
import android.os.Parcel;
import android.os.Parcelable;
public class Picture implements Parcelable {
public String pictureID, title, year, price, author;
public Bitmap picture;
public Picture(){
}
public int describeContents() {
return 0;
}
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(pictureID);
dest.writeString(title);
dest.writeString(year);
dest.writeString(price);
dest.writeString(author);
dest.writeParcelable(picture, flags);
}
protected Picture(Parcel in) {
pictureID = in.readString();
title = in.readString();
year = in.readString();
price = in.readString();
author = in.readString();
picture = in.readParcelable(Bitmap.class.getClassLoader());
}
public static final Parcelable.Creator<Picture> CREATOR = new Parcelable.Creator<Picture>() {
public Picture createFromParcel(Parcel source) {
return new Picture(source);
}
public Picture[] newArray(int size) {
return new Picture[size];
}
};
}
Picture firstPic = new Picture();
firstPic.pictureID = "1";
firstPic.title = "Mona Lisa";
firstPic.author = "Leonardo Da Vinci";
firstPic.price = "99999000";
firstPic.year = "2250";
firstPic.picture = BitmapFactory.decodeResource(getApplication().getResources(),R.drawable.monalisa);
Intent i = new Intent(getApplicationContext(),MainScreen.class);
i.putExtra("first",firstPic);
startActivity(i);
finish();
Picture currentPicture = new Picture();
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getIntent();
currentPicture = intent.getParcelableExtra("first");
}
编辑:我已经检查了关于Parcelable的所有其他主题,我也遵循了这些主题,但我无法真正找到错误所在的差异
编辑2:问题已解决。从一开始,一切都很正常,但出于某种原因,我在用数据填充对象和putExtra之间添加了一些代码,所以基本上我发送的是一个空对象”\_(ツ)_/“首先避免传递位图,它们可能太大,并将字符串
“First”
提取为常量,以接收活动
类:
public class ReceiverActivity extends ... {
public static final String EXTRA_FIRST = "first";
...
}
因此:
及
这个奇怪的事情已经发生在我身上了,我用这种方法解决了它。你到底有什么建议?在
i.putExtra()之后发布代码
您启动活动的地方添加到上述代码中尝试了它,但不幸的是,所有参数仍然为null。请尝试不使用位图。我相信这是默认值,因为我没有设置它。