通过Parcelable协议传递数据出现系列错误
07-14 00:42:37.414: E/AndroidRuntime(17687): FATAL EXCEPTION: main
07-14 00:42:37.414: E/AndroidRuntime(17687): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { (has extras) }} to activity {com.txrj.sms/com.txrj.sms.activity.CreateMessageActivity}: android.os.BadParcelableException: Parcelable protocol requires a Parcelable.Creator object called CREATOR on class com.txrj.sms.model.TxrjContact
07-14 00:42:37.414: E/AndroidRuntime(17687): at android.app.ActivityThread.deliverResults(ActivityThread.java:2997)
07-14 00:42:37.414: E/AndroidRuntime(17687): at android.app.ActivityThread.handleSendResult(ActivityThread.java:3040)
07-14 00:42:37.414: E/AndroidRuntime(17687): at android.app.ActivityThread.access$1100(ActivityThread.java:128)
07-14 00:42:37.414: E/AndroidRuntime(17687): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1191)
07-14 00:42:37.414: E/AndroidRuntime(17687): at android.os.Handler.dispatchMessage(Handler.java:99)
07-14 00:42:37.414: E/AndroidRuntime(17687): at android.os.Looper.loop(Looper.java:137)
07-14 00:42:37.414: E/AndroidRuntime(17687): at android.app.ActivityThread.main(ActivityThread.java:4517)
07-14 00:42:37.414: E/AndroidRuntime(17687): at java.lang.reflect.Method.invokeNative(Native Method)
07-14 00:42:37.414: E/AndroidRuntime(17687): at java.lang.reflect.Method.invoke(Method.java:511)
07-14 00:42:37.414: E/AndroidRuntime(17687): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:993)
07-14 00:42:37.414: E/AndroidRuntime(17687): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:760)
07-14 00:42:37.414: E/AndroidRuntime(17687): at dalvik.system.NativeStart.main(Native Method)
07-14 00:42:37.414: E/AndroidRuntime(17687): Caused by: android.os.BadParcelableException: Parcelable protocol requires a Parcelable.Creator object called CREATOR on class com.txrj.sms.model.TxrjContact
07-14 00:42:37.414: E/AndroidRuntime(17687): at android.os.Parcel.readParcelable(Parcel.java:1975)
07-14 00:42:37.414: E/AndroidRuntime(17687): at android.os.Parcel.readValue(Parcel.java:1854)
07-14 00:42:37.414: E/AndroidRuntime(17687): at android.os.Parcel.readListInternal(Parcel.java:2103)
07-14 00:42:37.414: E/AndroidRuntime(17687): at android.os.Parcel.readArrayList(Parcel.java:1544)
07-14 00:42:37.414: E/AndroidRuntime(17687): at android.os.Parcel.readValue(Parcel.java:1875)
07-14 00:42:37.414: E/AndroidRuntime(17687): at android.os.Parcel.readMapInternal(Parcel.java:2094)
07-14 00:42:37.414: E/AndroidRuntime(17687): at android.os.Bundle.unparcel(Bundle.java:223)
07-14 00:42:37.414: E/AndroidRuntime(17687): at android.os.Bundle.getParcelableArrayList(Bundle.java:1202)
07-14 00:42:37.414: E/AndroidRuntime(17687): at android.content.Intent.getParcelableArrayListExtra(Intent.java:4147)
07-14 00:42:37.414: E/AndroidRuntime(17687): at com.txrj.sms.activity.CreateMessageActivity.onActivityResult(CreateMessageActivity.java:276)
07-14 00:42:37.414: E/AndroidRuntime(17687): at android.app.Activity.dispatchActivityResult(Activity.java:4654)
07-14 00:42:37.414: E/AndroidRuntime(17687): at android.app.ActivityThread.deliverResults(ActivityThread.java:2993)
07-14 00:42:37.414: E/AndroidRuntime(17687): ... 11 more
参考示例
public class Person implements Parcelable
http://blog.csdn.net/knxw0001/article/details/9283867
在传递对象列表时,必须将对象实现Parcelable接口,但又必须创建CREATOR类
public static final Parcelable.Creator<TxrjContact> CREATOR = new Creator<TxrjContact>() {
@Override
public TxrjContact createFromParcel(Parcel source) {
TxrjContact contact = new TxrjContact();
contact.contactId = source.readInt();
contact.name = source.readString();
//contact.selected = source.readInt();
contact.hasNumber = source.readInt();
contact.time = source.readLong();
// contact.bSim
// contact.phoneList = source.readArrayList()
contact.sortKey = source.readString();
contact.photoId = source.readLong();
return contact;
}
@Override
public TxrjContact[] newArray(int size) {
return new TxrjContact[size];
}
};
在创建CREATOR类时,布尔值怎么读?List值怎么读?
以上代码执行果然还出现别的错误
07-14 00:55:43.301: E/AndroidRuntime(18040): FATAL EXCEPTION: main
07-14 00:55:43.301: E/AndroidRuntime(18040): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { (has extras) }} to activity {com.txrj.sms/com.txrj.sms.activity.CreateMessageActivity}: java.lang.RuntimeException: Parcel android.os.Parcel@41274138: Unmarshalling unknown type code 3145783 at offset 164
07-14 00:55:43.301: E/AndroidRuntime(18040): at android.app.ActivityThread.deliverResults(ActivityThread.java:2997)
07-14 00:55:43.301: E/AndroidRuntime(18040): at android.app.ActivityThread.handleSendResult(ActivityThread.java:3040)
07-14 00:55:43.301: E/AndroidRuntime(18040): at android.app.ActivityThread.access$1100(ActivityThread.java:128)
07-14 00:55:43.301: E/AndroidRuntime(18040): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1191)
07-14 00:55:43.301: E/AndroidRuntime(18040): at android.os.Handler.dispatchMessage(Handler.java:99)
07-14 00:55:43.301: E/AndroidRuntime(18040): at android.os.Looper.loop(Looper.java:137)
07-14 00:55:43.301: E/AndroidRuntime(18040): at android.app.ActivityThread.main(ActivityThread.java:4517)
07-14 00:55:43.301: E/AndroidRuntime(18040): at java.lang.reflect.Method.invokeNative(Native Method)
07-14 00:55:43.301: E/AndroidRuntime(18040): at java.lang.reflect.Method.invoke(Method.java:511)
07-14 00:55:43.301: E/AndroidRuntime(18040): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:993)
07-14 00:55:43.301: E/AndroidRuntime(18040): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:760)
07-14 00:55:43.301: E/AndroidRuntime(18040): at dalvik.system.NativeStart.main(Native Method)
07-14 00:55:43.301: E/AndroidRuntime(18040): Caused by: java.lang.RuntimeException: Parcel android.os.Parcel@41274138: Unmarshalling unknown type code 3145783 at offset 164
07-14 00:55:43.301: E/AndroidRuntime(18040): at android.os.Parcel.readValue(Parcel.java:1921)
07-14 00:55:43.301: E/AndroidRuntime(18040): at android.os.Parcel.readListInternal(Parcel.java:2103)
07-14 00:55:43.301: E/AndroidRuntime(18040): at android.os.Parcel.readArrayList(Parcel.java:1544)
07-14 00:55:43.301: E/AndroidRuntime(18040): at android.os.Parcel.readValue(Parcel.java:1875)
07-14 00:55:43.301: E/AndroidRuntime(18040): at android.os.Parcel.readMapInternal(Parcel.java:2094)
07-14 00:55:43.301: E/AndroidRuntime(18040): at android.os.Bundle.unparcel(Bundle.java:223)
07-14 00:55:43.301: E/AndroidRuntime(18040): at android.os.Bundle.getParcelableArrayList(Bundle.java:1202)
07-14 00:55:43.301: E/AndroidRuntime(18040): at android.content.Intent.getParcelableArrayListExtra(Intent.java:4147)
07-14 00:55:43.301: E/AndroidRuntime(18040): at com.txrj.sms.activity.CreateMessageActivity.onActivityResult(CreateMessageActivity.java:276)
07-14 00:55:43.301: E/AndroidRuntime(18040): at android.app.Activity.dispatchActivityResult(Activity.java:4654)
07-14 00:55:43.301: E/AndroidRuntime(18040): at android.app.ActivityThread.deliverResults(ActivityThread.java:2993)
07-14 00:55:43.301: E/AndroidRuntime(18040): ... 11 more
参考:
1. Android之进程间传递自定义类型参数
http://www.linuxidc.com/Linux/2011-08/41323.htm
2. Android进程间传递自定义类型参数实现Parcelable接口
http://www.2cto.com/kf/201303/193947.html
系统提供的Parcelable示例
A typical implementation of Parcelable is:
public class MyParcelable implements Parcelable {
private int mData; public int describeContents() {
return 0;
} public void writeToParcel(Parcel out, int flags) {
out.writeInt(mData);
} public static final Parcelable.Creator<MyParcelable> CREATOR
= new Parcelable.Creator<MyParcelable>() {
public MyParcelable createFromParcel(Parcel in) {
return new MyParcelable(in);
} public MyParcelable[] newArray(int size) {
return new MyParcelable[size];
}
}; private MyParcelable(Parcel in) {
mData = in.readInt();
}
}
07-14 06:54:58.766: E/AndroidRuntime(21250): FATAL EXCEPTION: main
07-14 06:54:58.766: E/AndroidRuntime(21250): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { (has extras) }} to activity {com.txrj.sms/com.txrj.sms.activity.CreateMessageActivity}: java.lang.RuntimeException: bad array lengths
07-14 06:54:58.766: E/AndroidRuntime(21250): at android.app.ActivityThread.deliverResults(ActivityThread.java:2997)
07-14 06:54:58.766: E/AndroidRuntime(21250): at android.app.ActivityThread.handleSendResult(ActivityThread.java:3040)
07-14 06:54:58.766: E/AndroidRuntime(21250): at android.app.ActivityThread.access$1100(ActivityThread.java:128)
07-14 06:54:58.766: E/AndroidRuntime(21250): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1191)
07-14 06:54:58.766: E/AndroidRuntime(21250): at android.os.Handler.dispatchMessage(Handler.java:99)
07-14 06:54:58.766: E/AndroidRuntime(21250): at android.os.Looper.loop(Looper.java:137)
07-14 06:54:58.766: E/AndroidRuntime(21250): at android.app.ActivityThread.main(ActivityThread.java:4517)
07-14 06:54:58.766: E/AndroidRuntime(21250): at java.lang.reflect.Method.invokeNative(Native Method)
07-14 06:54:58.766: E/AndroidRuntime(21250): at java.lang.reflect.Method.invoke(Method.java:511)
07-14 06:54:58.766: E/AndroidRuntime(21250): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:993)
07-14 06:54:58.766: E/AndroidRuntime(21250): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:760)
07-14 06:54:58.766: E/AndroidRuntime(21250): at dalvik.system.NativeStart.main(Native Method)
07-14 06:54:58.766: E/AndroidRuntime(21250): Caused by: java.lang.RuntimeException: bad array lengths
07-14 06:54:58.766: E/AndroidRuntime(21250): at android.os.Parcel.readBooleanArray(Parcel.java:619)
07-14 06:54:58.766: E/AndroidRuntime(21250): at com.txrj.sms.model.TxrjContact$1.createFromParcel(TxrjContact.java:38)
07-14 06:54:58.766: E/AndroidRuntime(21250): at com.txrj.sms.model.TxrjContact$1.createFromParcel(TxrjContact.java:1)
07-14 06:54:58.766: E/AndroidRuntime(21250): at android.os.Parcel.readParcelable(Parcel.java:1992)
07-14 06:54:58.766: E/AndroidRuntime(21250): at android.os.Parcel.readValue(Parcel.java:1854)
07-14 06:54:58.766: E/AndroidRuntime(21250): at android.os.Parcel.readListInternal(Parcel.java:2103)
07-14 06:54:58.766: E/AndroidRuntime(21250): at android.os.Parcel.readArrayList(Parcel.java:1544)
07-14 06:54:58.766: E/AndroidRuntime(21250): at android.os.Parcel.readValue(Parcel.java:1875)
07-14 06:54:58.766: E/AndroidRuntime(21250): at android.os.Parcel.readMapInternal(Parcel.java:2094)
07-14 06:54:58.766: E/AndroidRuntime(21250): at android.os.Bundle.unparcel(Bundle.java:223)
07-14 06:54:58.766: E/AndroidRuntime(21250): at android.os.Bundle.getParcelableArrayList(Bundle.java:1202)
07-14 06:54:58.766: E/AndroidRuntime(21250): at android.content.Intent.getParcelableArrayListExtra(Intent.java:4147)
07-14 06:54:58.766: E/AndroidRuntime(21250): at com.txrj.sms.activity.CreateMessageActivity.onActivityResult(CreateMessageActivity.java:276)
07-14 06:54:58.766: E/AndroidRuntime(21250): at android.app.Activity.dispatchActivityResult(Activity.java:4654)
07-14 06:54:58.766: E/AndroidRuntime(21250): at android.app.ActivityThread.deliverResults(ActivityThread.java:2993)
07-14 06:54:58.766: E/AndroidRuntime(21250): ... 11 more
问题原因:
boolean[] ba = new boolean[1];
source.readBooleanArray(ba);
改成
contact.selected = (source.readByte() == 1 ? true : false);
07-14 07:00:20.019: E/AndroidRuntime(21603): FATAL EXCEPTION: main
07-14 07:00:20.019: E/AndroidRuntime(21603): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { (has extras) }} to activity {com.txrj.sms/com.txrj.sms.activity.CreateMessageActivity}: java.lang.RuntimeException: Parcel android.os.Parcel@41c85dc8: Unmarshalling unknown type code 3145783 at offset 164
07-14 07:00:20.019: E/AndroidRuntime(21603): at android.app.ActivityThread.deliverResults(ActivityThread.java:2997)
07-14 07:00:20.019: E/AndroidRuntime(21603): at android.app.ActivityThread.handleSendResult(ActivityThread.java:3040)
07-14 07:00:20.019: E/AndroidRuntime(21603): at android.app.ActivityThread.access$1100(ActivityThread.java:128)
07-14 07:00:20.019: E/AndroidRuntime(21603): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1191)
07-14 07:00:20.019: E/AndroidRuntime(21603): at android.os.Handler.dispatchMessage(Handler.java:99)
07-14 07:00:20.019: E/AndroidRuntime(21603): at android.os.Looper.loop(Looper.java:137)
07-14 07:00:20.019: E/AndroidRuntime(21603): at android.app.ActivityThread.main(ActivityThread.java:4517)
07-14 07:00:20.019: E/AndroidRuntime(21603): at java.lang.reflect.Method.invokeNative(Native Method)
07-14 07:00:20.019: E/AndroidRuntime(21603): at java.lang.reflect.Method.invoke(Method.java:511)
07-14 07:00:20.019: E/AndroidRuntime(21603): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:993)
07-14 07:00:20.019: E/AndroidRuntime(21603): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:760)
07-14 07:00:20.019: E/AndroidRuntime(21603): at dalvik.system.NativeStart.main(Native Method)
07-14 07:00:20.019: E/AndroidRuntime(21603): Caused by: java.lang.RuntimeException: Parcel android.os.Parcel@41c85dc8: Unmarshalling unknown type code 3145783 at offset 164
07-14 07:00:20.019: E/AndroidRuntime(21603): at android.os.Parcel.readValue(Parcel.java:1921)
07-14 07:00:20.019: E/AndroidRuntime(21603): at android.os.Parcel.readListInternal(Parcel.java:2103)
07-14 07:00:20.019: E/AndroidRuntime(21603): at android.os.Parcel.readArrayList(Parcel.java:1544)
07-14 07:00:20.019: E/AndroidRuntime(21603): at com.txrj.sms.model.TxrjContact$1.createFromParcel(TxrjContact.java:41)
07-14 07:00:20.019: E/AndroidRuntime(21603): at com.txrj.sms.model.TxrjContact$1.createFromParcel(TxrjContact.java:1)
07-14 07:00:20.019: E/AndroidRuntime(21603): at android.os.Parcel.readParcelable(Parcel.java:1992)
07-14 07:00:20.019: E/AndroidRuntime(21603): at android.os.Parcel.readValue(Parcel.java:1854)
07-14 07:00:20.019: E/AndroidRuntime(21603): at android.os.Parcel.readListInternal(Parcel.java:2103)
07-14 07:00:20.019: E/AndroidRuntime(21603): at android.os.Parcel.readArrayList(Parcel.java:1544)
07-14 07:00:20.019: E/AndroidRuntime(21603): at android.os.Parcel.readValue(Parcel.java:1875)
07-14 07:00:20.019: E/AndroidRuntime(21603): at android.os.Parcel.readMapInternal(Parcel.java:2094)
07-14 07:00:20.019: E/AndroidRuntime(21603): at android.os.Bundle.unparcel(Bundle.java:223)
07-14 07:00:20.019: E/AndroidRuntime(21603): at android.os.Bundle.getParcelableArrayList(Bundle.java:1202)
07-14 07:00:20.019: E/AndroidRuntime(21603): at android.content.Intent.getParcelableArrayListExtra(Intent.java:4147)
07-14 07:00:20.019: E/AndroidRuntime(21603): at com.txrj.sms.activity.CreateMessageActivity.onActivityResult(CreateMessageActivity.java:276)
07-14 07:00:20.019: E/AndroidRuntime(21603): at android.app.Activity.dispatchActivityResult(Activity.java:4654)
07-14 07:00:20.019: E/AndroidRuntime(21603): at android.app.ActivityThread.deliverResults(ActivityThread.java:2993)
07-14 07:00:20.019: E/AndroidRuntime(21603): ... 11 more
问题原因:
contact.phoneList = source.readArrayList(TxrjPhone.class.getClassLoader());
改成
TxrjPhone[] phones = (TxrjPhone[]) source
.readParcelableArray(TxrjPhone.class.getClassLoader());
for (int i = 0; i < phones.length; i++) {
contact.phoneList.add(phones[i]);
}
07-14 07:06:23.363: E/Parcel(21880): Class not found when unmarshalling: ����com.txrj.sms.model.TxrjContact����N��{ContactId, e: java.lang.ClassNotFoundException: ����com.txrj.sms.model.TxrjContact����N��{ContactId
07-14 07:06:23.363: D/AndroidRuntime(21880): Shutting down VM
07-14 07:06:23.363: W/dalvikvm(21880): threadid=1: thread exiting with uncaught exception (group=0x40c0ba68)
07-14 07:06:23.383: E/AndroidRuntime(21880): FATAL EXCEPTION: main
07-14 07:06:23.383: E/AndroidRuntime(21880): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { (has extras) }} to activity {com.txrj.sms/com.txrj.sms.activity.CreateMessageActivity}: android.os.BadParcelableException: ClassNotFoundException when unmarshalling: ����com.txrj.sms.model.TxrjContact����N��{ContactId
07-14 07:06:23.383: E/AndroidRuntime(21880): at android.app.ActivityThread.deliverResults(ActivityThread.java:2997)
07-14 07:06:23.383: E/AndroidRuntime(21880): at android.app.ActivityThread.handleSendResult(ActivityThread.java:3040)
07-14 07:06:23.383: E/AndroidRuntime(21880): at android.app.ActivityThread.access$1100(ActivityThread.java:128)
07-14 07:06:23.383: E/AndroidRuntime(21880): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1191)
07-14 07:06:23.383: E/AndroidRuntime(21880): at android.os.Handler.dispatchMessage(Handler.java:99)
07-14 07:06:23.383: E/AndroidRuntime(21880): at android.os.Looper.loop(Looper.java:137)
07-14 07:06:23.383: E/AndroidRuntime(21880): at android.app.ActivityThread.main(ActivityThread.java:4517)
07-14 07:06:23.383: E/AndroidRuntime(21880): at java.lang.reflect.Method.invokeNative(Native Method)
07-14 07:06:23.383: E/AndroidRuntime(21880): at java.lang.reflect.Method.invoke(Method.java:511)
07-14 07:06:23.383: E/AndroidRuntime(21880): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:993)
07-14 07:06:23.383: E/AndroidRuntime(21880): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:760)
07-14 07:06:23.383: E/AndroidRuntime(21880): at dalvik.system.NativeStart.main(Native Method)
07-14 07:06:23.383: E/AndroidRuntime(21880): Caused by: android.os.BadParcelableException: ClassNotFoundException when unmarshalling: ����com.txrj.sms.model.TxrjContact����N��{ContactId
07-14 07:06:23.383: E/AndroidRuntime(21880): at android.os.Parcel.readParcelable(Parcel.java:1966)
07-14 07:06:23.383: E/AndroidRuntime(21880): at android.os.Parcel.readParcelableArray(Parcel.java:2008)
07-14 07:06:23.383: E/AndroidRuntime(21880): at com.txrj.sms.model.TxrjContact$1.createFromParcel(TxrjContact.java:42)
07-14 07:06:23.383: E/AndroidRuntime(21880): at com.txrj.sms.model.TxrjContact$1.createFromParcel(TxrjContact.java:1)
07-14 07:06:23.383: E/AndroidRuntime(21880): at android.os.Parcel.readParcelable(Parcel.java:1992)
07-14 07:06:23.383: E/AndroidRuntime(21880): at android.os.Parcel.readValue(Parcel.java:1854)
07-14 07:06:23.383: E/AndroidRuntime(21880): at android.os.Parcel.readListInternal(Parcel.java:2103)
07-14 07:06:23.383: E/AndroidRuntime(21880): at android.os.Parcel.readArrayList(Parcel.java:1544)
07-14 07:06:23.383: E/AndroidRuntime(21880): at android.os.Parcel.readValue(Parcel.java:1875)
07-14 07:06:23.383: E/AndroidRuntime(21880): at android.os.Parcel.readMapInternal(Parcel.java:2094)
07-14 07:06:23.383: E/AndroidRuntime(21880): at android.os.Bundle.unparcel(Bundle.java:223)
07-14 07:06:23.383: E/AndroidRuntime(21880): at android.os.Bundle.getParcelableArrayList(Bundle.java:1202)
07-14 07:06:23.383: E/AndroidRuntime(21880): at android.content.Intent.getParcelableArrayListExtra(Intent.java:4147)
07-14 07:06:23.383: E/AndroidRuntime(21880): at com.txrj.sms.activity.CreateMessageActivity.onActivityResult(CreateMessageActivity.java:276)
07-14 07:06:23.383: E/AndroidRuntime(21880): at android.app.Activity.dispatchActivityResult(Activity.java:4654)
07-14 07:06:23.383: E/AndroidRuntime(21880): at android.app.ActivityThread.deliverResults(ActivityThread.java:2993)
07-14 07:06:23.383: E/AndroidRuntime(21880): ... 11 more
问题语句:
dest.writeString(this.toString());
改成
dest.writeInt(contactId);
dest.writeString(name);
dest.writeByte(selected ? (byte)1 : (byte)0);
dest.writeInt(hasNumber);
dest.writeLong(time);
dest.writeByte(bSim ? (byte)1 : (byte)0);
dest.writeParcelableArray((Parcelable[])phoneList.toArray(), flags);
dest.writeString(sortKey);
dest.writeLong(photoId);
07-14 07:17:06.171: E/AndroidRuntime(22121): FATAL EXCEPTION: main
07-14 07:17:06.171: E/AndroidRuntime(22121): java.lang.ClassCastException: java.lang.Object[] cannot be cast to android.os.Parcelable[]
07-14 07:17:06.171: E/AndroidRuntime(22121): at com.txrj.sms.model.TxrjContact.writeToParcel(TxrjContact.java:165)
07-14 07:17:06.171: E/AndroidRuntime(22121): at android.os.Parcel.writeParcelable(Parcel.java:1156)
07-14 07:17:06.171: E/AndroidRuntime(22121): at android.os.Parcel.writeValue(Parcel.java:1075)
07-14 07:17:06.171: E/AndroidRuntime(22121): at android.os.Parcel.writeList(Parcel.java:524)
07-14 07:17:06.171: E/AndroidRuntime(22121): at android.os.Parcel.writeValue(Parcel.java:1097)
07-14 07:17:06.171: E/AndroidRuntime(22121): at android.os.Parcel.writeMapInternal(Parcel.java:493)
07-14 07:17:06.171: E/AndroidRuntime(22121): at android.os.Bundle.writeToParcel(Bundle.java:1612)
07-14 07:17:06.171: E/AndroidRuntime(22121): at android.os.Parcel.writeBundle(Parcel.java:507)
07-14 07:17:06.171: E/AndroidRuntime(22121): at android.content.Intent.writeToParcel(Intent.java:6279)
07-14 07:17:06.171: E/AndroidRuntime(22121): at android.app.ActivityManagerProxy.finishActivity(ActivityManagerNative.java:1845)
07-14 07:17:06.171: E/AndroidRuntime(22121): at android.app.Activity.finish(Activity.java:3709)
07-14 07:17:06.171: E/AndroidRuntime(22121): at com.txrj.sms.activity.ContactListActivity.onClick(ContactListActivity.java:272)
07-14 07:17:06.171: E/AndroidRuntime(22121): at android.view.View.performClick(View.java:3567)
07-14 07:17:06.171: E/AndroidRuntime(22121): at android.view.View$PerformClick.run(View.java:14224)
07-14 07:17:06.171: E/AndroidRuntime(22121): at android.os.Handler.handleCallback(Handler.java:605)
07-14 07:17:06.171: E/AndroidRuntime(22121): at android.os.Handler.dispatchMessage(Handler.java:92)
07-14 07:17:06.171: E/AndroidRuntime(22121): at android.os.Looper.loop(Looper.java:137)
07-14 07:17:06.171: E/AndroidRuntime(22121): at android.app.ActivityThread.main(ActivityThread.java:4517)
07-14 07:17:06.171: E/AndroidRuntime(22121): at java.lang.reflect.Method.invokeNative(Native Method)
07-14 07:17:06.171: E/AndroidRuntime(22121): at java.lang.reflect.Method.invoke(Method.java:511)
07-14 07:17:06.171: E/AndroidRuntime(22121): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:993)
07-14 07:17:06.171: E/AndroidRuntime(22121): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:760)
07-14 07:17:06.171: E/AndroidRuntime(22121): at dalvik.system.NativeStart.main(Native Method)
问题语句:
dest.writeParcelableArray((Parcelable[])phoneList.toArray(), flags);
改成
Parcelable[] value = new Parcelable[phoneList.size()];
for(int i=0;i<value.length;i++){
value[i] = phoneList.get(i);
}
07-14 07:22:45.932: E/AndroidRuntime(22442): FATAL EXCEPTION: main
07-14 07:22:45.932: E/AndroidRuntime(22442): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { (has extras) }} to activity {com.txrj.sms/com.txrj.sms.activity.CreateMessageActivity}: java.lang.ClassCastException: android.os.Parcelable[] cannot be cast to com.txrj.sms.model.TxrjPhone[]
07-14 07:22:45.932: E/AndroidRuntime(22442): at android.app.ActivityThread.deliverResults(ActivityThread.java:2997)
07-14 07:22:45.932: E/AndroidRuntime(22442): at android.app.ActivityThread.handleSendResult(ActivityThread.java:3040)
07-14 07:22:45.932: E/AndroidRuntime(22442): at android.app.ActivityThread.access$1100(ActivityThread.java:128)
07-14 07:22:45.932: E/AndroidRuntime(22442): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1191)
07-14 07:22:45.932: E/AndroidRuntime(22442): at android.os.Handler.dispatchMessage(Handler.java:99)
07-14 07:22:45.932: E/AndroidRuntime(22442): at android.os.Looper.loop(Looper.java:137)
07-14 07:22:45.932: E/AndroidRuntime(22442): at android.app.ActivityThread.main(ActivityThread.java:4517)
07-14 07:22:45.932: E/AndroidRuntime(22442): at java.lang.reflect.Method.invokeNative(Native Method)
07-14 07:22:45.932: E/AndroidRuntime(22442): at java.lang.reflect.Method.invoke(Method.java:511)
07-14 07:22:45.932: E/AndroidRuntime(22442): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:993)
07-14 07:22:45.932: E/AndroidRuntime(22442): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:760)
07-14 07:22:45.932: E/AndroidRuntime(22442): at dalvik.system.NativeStart.main(Native Method)
07-14 07:22:45.932: E/AndroidRuntime(22442): Caused by: java.lang.ClassCastException: android.os.Parcelable[] cannot be cast to com.txrj.sms.model.TxrjPhone[]
07-14 07:22:45.932: E/AndroidRuntime(22442): at com.txrj.sms.model.TxrjContact$1.createFromParcel(TxrjContact.java:41)
07-14 07:22:45.932: E/AndroidRuntime(22442): at com.txrj.sms.model.TxrjContact$1.createFromParcel(TxrjContact.java:1)
07-14 07:22:45.932: E/AndroidRuntime(22442): at android.os.Parcel.readParcelable(Parcel.java:1992)
07-14 07:22:45.932: E/AndroidRuntime(22442): at android.os.Parcel.readValue(Parcel.java:1854)
07-14 07:22:45.932: E/AndroidRuntime(22442): at android.os.Parcel.readListInternal(Parcel.java:2103)
07-14 07:22:45.932: E/AndroidRuntime(22442): at android.os.Parcel.readArrayList(Parcel.java:1544)
07-14 07:22:45.932: E/AndroidRuntime(22442): at android.os.Parcel.readValue(Parcel.java:1875)
07-14 07:22:45.932: E/AndroidRuntime(22442): at android.os.Parcel.readMapInternal(Parcel.java:2094)
07-14 07:22:45.932: E/AndroidRuntime(22442): at android.os.Bundle.unparcel(Bundle.java:223)
07-14 07:22:45.932: E/AndroidRuntime(22442): at android.os.Bundle.getParcelableArrayList(Bundle.java:1202)
07-14 07:22:45.932: E/AndroidRuntime(22442): at android.content.Intent.getParcelableArrayListExtra(Intent.java:4147)
07-14 07:22:45.932: E/AndroidRuntime(22442): at com.txrj.sms.activity.CreateMessageActivity.onActivityResult(CreateMessageActivity.java:276)
07-14 07:22:45.932: E/AndroidRuntime(22442): at android.app.Activity.dispatchActivityResult(Activity.java:4654)
07-14 07:22:45.932: E/AndroidRuntime(22442): at android.app.ActivityThread.deliverResults(ActivityThread.java:2993)
07-14 07:22:45.932: E/AndroidRuntime(22442): ... 11 more
问题语句:
TxrjPhone[] phones = (TxrjPhone[]) source
.readParcelableArray(TxrjPhone.class.getClassLoader());
for (int i = 0; i < phones.length; i++) {
contact.phoneList.add(phones[i]);
}
改成
Parcelable[] phones = source
.readParcelableArray(TxrjPhone.class.getClassLoader());
for (int i = 0; i < phones.length; i++) {
contact.phoneList.add((TxrjPhone)phones[i]);
}
OK!!!
Parcelable类型的数据传递成功!
通过Parcelable协议传递数据出现系列错误的更多相关文章
- 背水一战 Windows 10 (101) - 应用间通信: 通过协议打开指定的 app 并传递数据以及获取返回数据, 将本 app 沙盒内的文件共享给其他 app 使用
[源码下载] 背水一战 Windows 10 (101) - 应用间通信: 通过协议打开指定的 app 并传递数据以及获取返回数据, 将本 app 沙盒内的文件共享给其他 app 使用 作者:weba ...
- MVC学习系列3--怎么从控制器向视图传递数据
在MVC中,从控制器到视图,传递数据,可以使用 ViewData 和 ViewBag:同样从视图到控制器,传递数据,可以使用Post,QueryString,或者隐藏域:最后从控制器到控制器,传递数据 ...
- Controller之间传递数据:协议传值
http://itjoy.org/?p=416 前边介绍过从第一个页面传递数据到第二个页面,那么反过来呢我们该如何操作?还是同一个例子,将第二个页面的字符串传递到第一个页面显示出来,这中形式就可以使用 ...
- Activity之间传递数据或数据包Bundle,传递对象,对象序列化,对象实现Parcelable接口
package com.gaojinhua.android.activitymsg; import android.content.Intent; import android.os.Bundle; ...
- 【ASP.NET MVC系列】浅谈ASP.NET MVC 视图与控制器传递数据
ASP.NET MVC系列文章 [01]浅谈Google Chrome浏览器(理论篇) [02]浅谈Google Chrome浏览器(操作篇)(上) [03]浅谈Google Chrome浏览器(操作 ...
- JavaScript onclick传递对象参数(easyui传递一行数据时)错误:uncaught SyntaxError: Unexpected identifier
JavaScript onclick传递对象参数(easyui传递一行数据时)错误:uncaught SyntaxError: Unexpected identifier 博主遇到的是用onclick ...
- 【iOS系列】-UINavigationController的使用(Segue传递数据)
[iOS系列]-UINavigationController的使用 UINavigationController是以以栈(先进后出)的形式保存子控制器, 常用属性: UINavigationItem有 ...
- LabVIEW系列——错误簇的传递
从以下示例可以得出结论: 1.图一出现的三种错误,分别位于打开/创建/替换文件函数,写入文本文件函数,读取文件文件函数.说明三个函数都被运行了. 2.图二只出现了一种错误,位于打开/创建/替换文件函数 ...
- EF5+MVC4系列(7) 后台SelectListItem传值给前台显示Select下拉框;后台Action接收浏览器传值的4种方式; 后台Action向前台View视图传递数据的四种方式(ViewDate,TempDate,ViewBag,Model (实际是ViewDate.Model传值))
一:后台使用SelectListItem 传值给前台显示Select下拉框 我们先来看数据库的订单表,里面有3条订单,他们的用户id对应了 UserInfo用户表的数据,现在我们要做的是添加一个Ord ...
随机推荐
- LeetCode题解:Rotate List
Rotate List Given a list, rotate the list to the right by k places, where k is non-negative. For exa ...
- 解决ASP.NET中的各种乱码问题
解决ASP.NET中的各种乱码问题 阅读目录 开始 页面显示乱码问题 AJAX提交的数据乱码问题 JavaScript中正确的URL编码方式 ASP.NET中正确的URL编码方式 正确的URL编码方式 ...
- Intent 简介 结构 传递数据 常见Action 常量 MD
Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...
- 机器学习、NLP、Python和Math最好的150余个教程(建议收藏)
编辑 | MingMing 尽管机器学习的历史可以追溯到1959年,但目前,这个领域正以前所未有的速度发展.最近,我一直在网上寻找关于机器学习和NLP各方面的好资源,为了帮助到和我有相同需求的人,我整 ...
- xgboost入门与实战
xgboost入门与实战(实战调参篇) https://blog.csdn.net/sb19931201/article/details/52577592 前言 前面几篇博文都在学习原理知识,是时候上 ...
- Git 常用命令 思维导图(转)
Git 是一个很强大的分布式版本控制系统.它不但适用于管理大型开源软件的源代码,管理私人的文档和源代码也有很多优势. 本来想着只把最有用.最常用的 Git 命令记下来,但是总觉得这个也挺有用.那个也用 ...
- 【转】Andorid中Intent的使用-返回数据给上一个活动
第一个Activity A启动另外一个Activity B,B返回数据给A ============================================================= ...
- 关于Chrome浏览器(Chrome Stable、 Chrome Canary 、Chromium)
作为开发者,web浏览器一般最常用的可能是Chrome浏览器.但其实Chrome浏览器还有别的一些版本.如:Chrome Stable. Chrome Canary .Chromium.大部分人一般用 ...
- 基于FPGA的超声波测距(一)
硬件平台:CycloneII EP2C5Q208C8+DYP-ME007 1:超声波原理 DYP-ME007超声波测距模块可提供3cm--3.5m的非接触式距离感测功能,图1为DYP-ME007外观, ...
- hadoop multipleoutputs
http://grepalex.com/2013/05/20/multipleoutputs-part1/ http://grepalex.com/2013/07/16/multipleoutputs ...