android - 如何在 android 中使用 intent 传递 parcelable 变量值?

标签 android android-intent badparcelableexception

我的应用程序在使用 Intent 操作传递可分包值时出现 BadParcelableException。如何传递 parcelable 值并在另一个类中检索它们?

下面是我的代码:

public MySample mySample;

  public class MySample implements Parcelable {
        private boolean galleryPicker = false;
        private boolean gallery = false;
        private ImageButton button;
        private View view;

        public ImageButton getButton() {
            return button;
        }

        public void setPostButtonItem(ImageButton button) {
            this.button= button;
        }
        @Override
        public void writeToParcel(Parcel dest, int flags) {
            dest.writeStringArray(new String[] {});
        }

        public int describeContents(){
            return 0;
        }

Intent Action :

public class Popup// (Actually I call this class from my BaseActivity)
{
    public Popup(Activity activity,ActionBar action)
    {
        MySample mySample= new MySample();
        sample.data.setStatus=true;
        no= (TextView) popupView.findViewById(R.id.no);
        yes= (TextView) popupView.findViewById(R.id.yes);
        mySample.gallery = true;
        mySample.count = true;
        context = getContext();
        Intent intent = new Intent(context, NextActivity.class);
        intent.putExtra("sample", mySample);
        context.startActivity(intent);
    }
}

在另一个类中提取 Intent :

private MySample mySample;
Bundle data = getIntent().getExtras();
this.mySample= data.getParcelable("sample");

异常(exception):

Caused by: android.os.BadParcelableException: Parcelable protocol requires a Parcelable.Creator object called CREATOR on class

最佳答案

试试这个

 public class MySample implements Parcelable {

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

        public MySample[] newArray(int size) {
            return new MySample[size];
        }
    };
    private boolean galleryPicker = false;
    private boolean gallery = false;

    public MySample(Parcel in) {
        boolean[] temp = new boolean[2];
        in.readBooleanArray(temp);
        galleryPicker = temp[0];
        gallery = temp[1];
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeBooleanArray(new boolean[]{galleryPicker, gallery});
    }
}

关于android - 如何在 android 中使用 intent 传递 parcelable 变量值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28878669/

相关文章:

android - 在 Activity 之间传递可打包对象

android - 检查intent是调用还是默认启动Activity

android - HTML5 音频在 Android 平台上的 PhoneGap 中不起作用

android - 无法弄清楚如何获取 View /布局宽度/高度

android - 使用 Jabber ID 的 Facebook 连接

android - 如何永久存储可打包的自定义对象?

java - android 中的 putExtra() 和 getBooleanExtra()

Android Parcelable 订单

android - 实现 Parcelable 的正确方法

java - 三星设备上的 Android Camera2 问题