我试图将parcelable数组从MainActivity转移到SecondActivity,但遇到了一些奇怪的问题 我定义的可分类类如下所述:
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);
}
}
}
在MainActivity中,我放了一个人和一个人阵列:
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);
在SecondActivity中,我得到了人和人阵列:
Intent intent = getIntent();
Bundle bundle = intent.getExtras();
mPerson = bundle.getParcelable("p");
Parcelable[] ps = bundle.getParcelableArray("persons");
到目前为止一切顺利
[但!!]当我编码如下:
在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);
在SecondActivity中,
Intent intent = getIntent();
Bundle bundle = intent.getExtras();
mPerson = bundle.getParcelable("person"); // problem happened here
Parcelable[] ps = bundle.getParcelableArray("persons");
我遇到了运行时异常,程序崩溃了:
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
我真的不知道为什么......我只是改变了&#34; p&#34;到了#34;人&#34;坏事发生了。 &LT; _&LT;
答案 0 :(得分:1)
不要使用ParcelableArray
,它实际上并不是你想要的。使用writeTypedArray
和readTypedArray
方法。
请查看我的回答here,了解有关如何执行该操作的详细信息。
答案 1 :(得分:0)
试试这个:
if(getIntent().getExtras() != null)
{
}
可能是您的MainActivity没有第一次获取意图数据。
答案 2 :(得分:0)
检查清单文件中的应用程序标记中是否添加活动标记。 喜欢
<activity
android:name=".MainActivity"
android:label="@string/app_name" >