zoukankan      html  css  js  c++  java
  • 通过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类型的数据传递成功!

  • 相关阅读:
    接口测试(基础知识)
    MapReduce的方式进行HBase向HDFS导入和导出
    HBase的JavaAPI操作
    Maven中settings.xml的配置项说明
    Eclipse使用Maven创建普通Java工程时错误:Could not resolve archetype org.apache.maven.archetypes:maven-archetype-quickstart:RELEASE from any of the configured repositories.
    ip 子网掩码 网关 DNS
    IP地址,子网掩码、默认网关,DNS理论解释
    IP地址,子网掩码,默认网关,路由,形象生动阐述
    Hive的JDBC使用&并把JDBC放置后台运行
    Hive中自定义函数
  • 原文地址:https://www.cnblogs.com/fengzhblog/p/3188971.html
Copyright © 2011-2022 走看看