Android put and get parcelable array error

1.2k views Asked by At

I was trying to transfer parcelable arrays from MainActivity to SecondActivity, but met some strange problems.
The parcelable class I defined is described below:

public class Person implements Parcelable {
    public int id;
    public String name;
    public byte[] key;

    public Person() {}

    public Person(int id_, String name_, byte[] key_) {
        id = id_;
        name = name_;
        key = key_;
    }

    private Person(Parcel in) {
        id = in.readInt();
        name = in.readString();
        int kl = in.readInt();
        if (kl > 0) {
            key = new byte[kl];
            in.readByteArray(key);
        }
    }

    public static Parcelable.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];
        }
    };

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

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeInt(id);
        dest.writeString(name);
        if (key != null) {
            dest.writeInt(key.length);
            dest.writeByteArray(key);
        } else {
            dest.writeInt(0);
        }
    }
}

In MainActivity, I put single person and a persons array:

Intent intent = new Intent(this, SecondActivity.class);
Bundle bundle = new Bundle();
bundle.putParcelable("p", mPerson); // pay attention to this
bundle.putParcelableArray("persons", mPersons);
intent.putExtras(bundle);
startActivity(intent);

In SecondActivity, I got the person and persons array:

Intent intent = getIntent();
Bundle bundle = intent.getExtras();
mPerson = bundle.getParcelable("p");
Parcelable[] ps = bundle.getParcelableArray("persons");

So far so good.
[But!!] When I coded as belows:
In MainActivity,

Intent intent = new Intent(this, SecondActivity.class);
Bundle bundle = new Bundle();
bundle.putParcelable("person", mPerson); // code changed here
bundle.putParcelableArray("persons", mPersons);
intent.putExtras(bundle);
startActivity(intent);

In SecondActivity,

Intent intent = getIntent();
Bundle bundle = intent.getExtras();
mPerson = bundle.getParcelable("person"); // problem happened here
Parcelable[] ps = bundle.getParcelableArray("persons");

I got a runtime exception and the program crashed:

06-09 13:52:01.458: E/AndroidRuntime(27761): FATAL EXCEPTION: main
06-09 13:52:01.458: E/AndroidRuntime(27761): Process: com.example.parceabletest, PID: 27761
06-09 13:52:01.458: E/AndroidRuntime(27761): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.parceabletest/com.example.parceabletest.SecondActivity}: java.lang.RuntimeException: Parcel android.os.Parcel@44b7ed40: Unmarshalling unknown type code 6619248 at offset 140
06-09 13:52:01.458: E/AndroidRuntime(27761):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2248)
06-09 13:52:01.458: E/AndroidRuntime(27761):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2298)
06-09 13:52:01.458: E/AndroidRuntime(27761):    at android.app.ActivityThread.access$800(ActivityThread.java:144)
06-09 13:52:01.458: E/AndroidRuntime(27761):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1246)
06-09 13:52:01.458: E/AndroidRuntime(27761):    at android.os.Handler.dispatchMessage(Handler.java:102)
06-09 13:52:01.458: E/AndroidRuntime(27761):    at android.os.Looper.loop(Looper.java:212)
06-09 13:52:01.458: E/AndroidRuntime(27761):    at android.app.ActivityThread.main(ActivityThread.java:5151)
06-09 13:52:01.458: E/AndroidRuntime(27761):    at java.lang.reflect.Method.invokeNative(Native Method)
06-09 13:52:01.458: E/AndroidRuntime(27761):    at java.lang.reflect.Method.invoke(Method.java:515)
06-09 13:52:01.458: E/AndroidRuntime(27761):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
06-09 13:52:01.458: E/AndroidRuntime(27761):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:684)
06-09 13:52:01.458: E/AndroidRuntime(27761):    at dalvik.system.NativeStart.main(Native Method)
06-09 13:52:01.458: E/AndroidRuntime(27761): Caused by: java.lang.RuntimeException: Parcel android.os.Parcel@44b7ed40: Unmarshalling unknown type code 6619248 at offset 140
06-09 13:52:01.458: E/AndroidRuntime(27761):    at android.os.Parcel.readValue(Parcel.java:2112)
06-09 13:52:01.458: E/AndroidRuntime(27761):    at android.os.Parcel.readArrayMapInternal(Parcel.java:2346)
06-09 13:52:01.458: E/AndroidRuntime(27761):    at android.os.Bundle.unparcel(Bundle.java:249)
06-09 13:52:01.458: E/AndroidRuntime(27761):    at android.os.Bundle.getParcelable(Bundle.java:1206)
06-09 13:52:01.458: E/AndroidRuntime(27761):    at com.example.parceabletest.SecondActivity.onCreate(SecondActivity.java:27)
06-09 13:52:01.458: E/AndroidRuntime(27761):    at android.app.Activity.performCreate(Activity.java:5231)
06-09 13:52:01.458: E/AndroidRuntime(27761):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
06-09 13:52:01.458: E/AndroidRuntime(27761):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2212)
06-09 13:52:01.458: E/AndroidRuntime(27761):    ... 11 more

I really don't know why... I just changed the parcelable key from "p" to "person" and bad things happened. <_<

3

There are 3 answers

0
EpicPandaForce On

Don't use ParcelableArray, it doesn't actually do what you want. Use writeTypedArray and readTypedArray methods.

Look at my answer here for more info on how to do that.

0
Khushal Chouhan On

Try this:

if(getIntent().getExtras() != null)
{

}

May be your MainActivity is not getting the intent data first time.

0
Hiren Vaghela On

check add activity tag in application tag in manifest file or not. like

<activity
        android:name=".MainActivity"
        android:label="@string/app_name" >