0

我正在尝试将 XMPPConnection 对象从一个 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)

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

4

1 回答 1

2

亲爱的,第一行清楚地说明了问题

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

你的 XMPPConnection 不是一个可打包的实例。如果您班级中的某件事不可打包,那么您将无法将其作为可打包或可序列化的实例传递给另一个活动

于 2013-06-04T12:53:50.223 回答