无法包裹地图

时间:2013-06-04 10:55:16

标签: android android-intent marshalling parcelable

我正在尝试将XMPPConnection对象从一个Activity传递到另一个Activity。我编写了一个实现Parcelable并使用HashMap存储对象的类。

这是我的代码:

class MyParcelable implements Parcelable {


     Map val=new HashMap<String, XMPPConnection>();

    public XMPPConnection getConnection(String s){
        return (XMPPConnection) val.get(s);
    }

    public void setConnection(String s,XMPPConnection conn){
        val.put(s, conn);
    }

    MyParcelable() {

        val=new HashMap<String, XMPPConnection>();
    }

    public MyParcelable(Parcel in) {
        in.readMap(val, MyParcelable.class.getClassLoader());
    }

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

    @Override
    public void writeToParcel(Parcel outParcel, int flags) {

        outParcel.writeMap(val);
    }

    public static final Parcelable.Creator<MyParcelable> CREATOR = new Parcelable.Creator<MyParcelable>() {

        @Override
        public MyParcelable createFromParcel(Parcel in) {
            return new MyParcelable(in);
        }

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

在创建Intent时,我这样做:

MyParcelable pr=new MyParcelable();
        pr.setConnection("connectionobj", connection);
        Intent i=new Intent(TestActivity2.this,TestActivity.class);
        i.putExtra("connection", pr);
        startActivity(i);

在获取额外内容时,我正在这样做:

Bundle b = getIntent().getExtras();
MyParcelable object = b.getParcelable("connection");
this.connection=object.getConnection("connectionobj");

但每次我都会因此而异常:

outParcel.writeMap(val);

例外:

06-04 16:04:23.443: E/AndroidRuntime(708): FATAL EXCEPTION: main
06-04 16:04:23.443: E/AndroidRuntime(708): java.lang.RuntimeException: Parcel: unable to marshal value org.jivesoftware.smack.XMPPConnection@44f577b8
06-04 16:04:23.443: E/AndroidRuntime(708):  at android.os.Parcel.writeValue(Parcel.java:1116)
06-04 16:04:23.443: E/AndroidRuntime(708):  at android.os.Parcel.writeMapInternal(Parcel.java:479)
06-04 16:04:23.443: E/AndroidRuntime(708):  at android.os.Parcel.writeMap(Parcel.java:463)
06-04 16:04:23.443: E/AndroidRuntime(708):  at com.demo.xmppchat.MyParcelable.writeToParcel(TestActivity2.java:1330)
06-04 16:04:23.443: E/AndroidRuntime(708):  at android.os.Parcel.writeParcelable(Parcel.java:1135)
06-04 16:04:23.443: E/AndroidRuntime(708):  at android.os.Parcel.writeValue(Parcel.java:1054)
06-04 16:04:23.443: E/AndroidRuntime(708):  at android.os.Parcel.writeMapInternal(Parcel.java:479)
06-04 16:04:23.443: E/AndroidRuntime(708):  at android.os.Bundle.writeToParcel(Bundle.java:1552)
06-04 16:04:23.443: E/AndroidRuntime(708):  at android.os.Parcel.writeBundle(Parcel.java:493)
06-04 16:04:23.443: E/AndroidRuntime(708):  at android.content.Intent.writeToParcel(Intent.java:5438)
06-04 16:04:23.443: E/AndroidRuntime(708):  at android.app.ActivityManagerProxy.startActivity(ActivityManagerNative.java:1288)
06-04 16:04:23.443: E/AndroidRuntime(708):  at android.app.Instrumentation.execStartActivity(Instrumentation.java:1373)
06-04 16:04:23.443: E/AndroidRuntime(708):  at android.app.Activity.startActivityForResult(Activity.java:2817)
06-04 16:04:23.443: E/AndroidRuntime(708):  at android.app.Activity.startActivity(Activity.java:2923)
06-04 16:04:23.443: E/AndroidRuntime(708):  at com.demo.xmppchat.TestActivity2.sendmessages(TestActivity2.java:955)
06-04 16:04:23.443: E/AndroidRuntime(708):  at com.demo.xmppchat.IQDepartmentProvider2$1.run(TestActivity2.java:1040)
06-04 16:04:23.443: E/AndroidRuntime(708):  at android.os.Handler.handleCallback(Handler.java:587)
06-04 16:04:23.443: E/AndroidRuntime(708):  at android.os.Handler.dispatchMessage(Handler.java:92)
06-04 16:04:23.443: E/AndroidRuntime(708):  at android.os.Looper.loop(Looper.java:123)
06-04 16:04:23.443: E/AndroidRuntime(708):  at android.app.ActivityThread.main(ActivityThread.java:4627)
06-04 16:04:23.443: E/AndroidRuntime(708):  at java.lang.reflect.Method.invokeNative(Native Method)
06-04 16:04:23.443: E/AndroidRuntime(708):  at java.lang.reflect.Method.invoke(Method.java:521)
06-04 16:04:23.443: E/AndroidRuntime(708):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
06-04 16:04:23.443: E/AndroidRuntime(708):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
06-04 16:04:23.443: E/AndroidRuntime(708):  at dalvik.system.NativeStart.main(Native Method)

我在做什么错?任何输入都会有所帮助。提前谢谢。

1 个答案:

答案 0 :(得分:2)

亲爱的,第一行显然是在说明问题

06-04 16:04:23.443: E/AndroidRuntime(708): java.lang.RuntimeException: Parcel: unable to marshal value org.jivesoftware.smack.XMPPConnection@44f577b8

你的XMPPConnection不是一个可以分辨的意思。如果你班级中的一件事物不可分割,那么你将无法传递给另一个活动作为可分割或可序列化的实例