BadParcelableException

时间:2015-05-22 17:54:02

标签: java android android-activity parcelable transfer

我正在尝试将主活动中的一些数据包含到另一个活动中,并在ListView中显示数据。我的MainActivity中有一个按钮,用于创建和意图并将数据发送到其他活动。但是,只要我按下按钮,应用程序就会崩溃并发生BadParcelableException。有人可以向我解释为什么会这样吗?

这是我在MainActivity中的代码:

public class MainActivity extends Activity {
private Button mButton;
public static String EXTRA_KEY = "key_extra";
private Person[] mPersons;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mPersons = new Person[1];

    mPersons[0] = new Person("GEORGI",23);
    mPersons[0] = new Person("Mariya",21);

    mButton = (Button) findViewById(R.id.parcelBTN);
    mButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(MainActivity.this,ReceiveParcelActivity.class);
            intent.putExtra(EXTRA_KEY,mPersons);
            startActivity(intent);
        }
    });
}

这是第二个活动:

public class ReceiveParcelActivity extends ListActivity {
private Person[] mPersons;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_receive_parcel);

    Intent intent = getIntent();
    Parcelable[] parcelables = intent.getParcelableArrayExtra(MainActivity.EXTRA_KEY);

    mPersons = Arrays.copyOf(parcelables,parcelables.length,Person[].class);

    ArrayAdapter<Person> adapter = new ArrayAdapter<Person>(this,android.R.layout.simple_list_item_1,mPersons);

    setListAdapter(adapter);
}

这是实现Parcelable接口的Person类

public class Person implements Parcelable {
private String mName;
private int mAge;

public Person(String name, int age) {
    mName = name;
    mAge = age;
}

public Person(Parcel parcel) {
    mName = parcel.readString();
    mAge = parcel.readInt();
}

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

@Override
public void writeToParcel(Parcel dest, int flags) {
    dest.writeString(mName);
    dest.writeInt(mAge);
}

private static Creator<Person> CREATOR = new Creator<Person>() {
    @Override
    public Person createFromParcel(Parcel source) {
        return new Person(source);
    }

    @Override
    public Person[] newArray(int size) {
        return new Person[size];
    }
};

2 个答案:

答案 0 :(得分:0)

我知道出了什么问题。我宣称造物主是私人的。在宣布公开后,它解决了问题。但是,现在我不知道如何将数据呈现到ListActivity中,我的意思是,现在只有一行,那就是Person对象的内存地址。 enter image description here

答案 1 :(得分:0)

请尝试遵循此模式,而是隐式定义您的类中指定的所有字段:

public class Person implements Parcelable {
    private String mName;
    private int mAge;

    public Person(String name, int age) {
        mName = name;
        mAge = age;
    }

    // ##### PARCELABLE ROUTINES #####
    private static final String KEY_NAME = "key_name";
    private static final String KEY_AGE = "key_age";

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

    @Override
    public void writeToParcel(Parcel parcel, int i) {
        Bundle b = new Bundle();
        b.putString(KEY_NAME, mName);
        b.putInt(KEY_AGE, mAge);
        parcel.writeBundle(b);
    }

    public static final Parcelable.Creator<Person> CREATOR = new Creator<Person>() {
        @Override
        public Person createFromParcel(Parcel source) {
            Bundle b = source.readBundle();
            b.setClassLoader(getClass().getClassLoader());
            String name = b.getString(KEY_NAME);
            int age = b.getInt(KEY_AGE);

            return new Person(name, age);
        }

        @Override
        public Person[] newArray(int size) {
            return new Person[size];
        }
    };
}