2

在我的程序中,我必须在两个活动之间传递一个自定义对象。在第一个活动中单击列表视图的项目后,第二个活动开始。第一个活动的 OnItemClick 中的代码是:

public void onItemClick(AdapterView<?> arg0, View v,
                    int position, long arg3) {

                Intent userProfileIntent = new Intent(v.getContext(),
                        UserProfileActivity.class);
                Bundle bundle = new Bundle();
                bundle.putSerializable("contact", new ContactSerializer(
                        sortContacts.get(position)));
                userProfileIntent.putExtras(bundle);
                v.getContext().startActivity(userProfileIntent);

            }

第二个活动的代码是:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.userprofile_layout);

    ContactSerializer serializer = (ContactSerializer) getIntent()
            .getSerializableExtra("contact");

    CustomObj contact = serializer.getContact();

可序列化对象的代码是:

public class ContactSerializer implements Serializable {

private CustomObj contact;

public ContactSerializer(CustomObj contact) {
    this.contact = contact;
}

public CustomObj getContact() {
    return contact;
}

}

我的 LogCat 是:

07-10 13:07:57.777: E/AndroidRuntime(1333): FATAL EXCEPTION: main
07-10 13:07:57.777: E/AndroidRuntime(1333): java.lang.RuntimeException: Parcelable encountered IOException writing serializable object (name = com.app.ContactSerializer)
07-10 13:07:57.777: E/AndroidRuntime(1333):     at android.os.Parcel.writeSerializable(Parcel.java:1279)
07-10 13:07:57.777: E/AndroidRuntime(1333):     at android.os.Parcel.writeValue(Parcel.java:1233)
07-10 13:07:57.777: E/AndroidRuntime(1333):     at android.os.Parcel.writeMapInternal(Parcel.java:591)
07-10 13:07:57.777: E/AndroidRuntime(1333):     at android.os.Bundle.writeToParcel(Bundle.java:1627)
07-10 13:07:57.777: E/AndroidRuntime(1333):     at android.os.Parcel.writeBundle(Parcel.java:605)
07-10 13:07:57.777: E/AndroidRuntime(1333):     at android.content.Intent.writeToParcel(Intent.java:6660)
07-10 13:07:57.777: E/AndroidRuntime(1333):     at android.app.ActivityManagerProxy.startActivity(ActivityManagerNative.java:1865)
07-10 13:07:57.777: E/AndroidRuntime(1333):     at android.app.Instrumentation.execStartActivity(Instrumentation.java:1412)
07-10 13:07:57.777: E/AndroidRuntime(1333):     at android.app.Activity.startActivityForResult(Activity.java:3370)
07-10 13:07:57.777: E/AndroidRuntime(1333):     at android.app.Activity.startActivityForResult(Activity.java:3331)
07-10 13:07:57.777: E/AndroidRuntime(1333):     at android.app.Activity.startActivity(Activity.java:3566)
07-10 13:07:57.777: E/AndroidRuntime(1333):     at android.app.Activity.startActivity(Activity.java:3534)
07-10 13:07:57.777: E/AndroidRuntime(1333):     at com.app.MyProfileActivity$ContactsLoader$1.onItemClick(MyProfileActivity.java:153)
07-10 13:07:57.777: E/AndroidRuntime(1333):     at android.widget.AdapterView.performItemClick(AdapterView.java:298)
07-10 13:07:57.777: E/AndroidRuntime(1333):     at android.widget.AbsListView.performItemClick(AbsListView.java:1100)
07-10 13:07:57.777: E/AndroidRuntime(1333):     at android.widget.AbsListView$PerformClick.run(AbsListView.java:2749)
07-10 13:07:57.777: E/AndroidRuntime(1333):     at android.widget.AbsListView$1.run(AbsListView.java:3423)
07-10 13:07:57.777: E/AndroidRuntime(1333):     at android.os.Handler.handleCallback(Handler.java:725)
07-10 13:07:57.777: E/AndroidRuntime(1333):     at android.os.Handler.dispatchMessage(Handler.java:92)
07-10 13:07:57.777: E/AndroidRuntime(1333):     at android.os.Looper.loop(Looper.java:137)
07-10 13:07:57.777: E/AndroidRuntime(1333):     at android.app.ActivityThread.main(ActivityThread.java:5041)
07-10 13:07:57.777: E/AndroidRuntime(1333):     at java.lang.reflect.Method.invokeNative(Native Method)
070 13:07:57.777: E/AndroidRuntime(1333):   at java.lang.reflect.Method.invoke(Method.java:511)
07-10 13:07:57.777: E/AndroidRuntime(1333):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
07-10 13:07:57.777: E/AndroidRuntime(1333):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
07-10 13:07:57.777: E/AndroidRuntime(1333):     at dalvik.system.NativeStart.main(Native Method)
07-10 13:07:57.777: E/AndroidRuntime(1333): Caused by: java.io.NotSerializableException: com.CustomObj
07-10 13:07:57.777: E/AndroidRuntime(1333):     at java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1364)
07-10 13:07:57.777: E/AndroidRuntime(1333):     at java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1671)
07-10 13:07:57.777: E/AndroidRuntime(1333):     at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1517)
07-10 13:07:57.777: E/AndroidRuntime(1333):     at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1481)
07-10 13:07:57.777: E/AndroidRuntime(1333):     at java.io.ObjectOutputStream.writeFieldValues(ObjectOutputStream.java:979)
07-10 13:07:57.777: E/AndroidRuntime(1333):     at java.io.ObjectOutputStream.defaultWriteObject(ObjectOutputStream.java:368)
07-10 13:07:57.777: E/AndroidRuntime(1333):     at java.io.ObjectOutputStream.writeHierarchy(ObjectOutputStream.java:1074)
07-10 13:07:57.777: E/AndroidRuntime(1333):     at java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1404)
07-10 13:07:57.777: E/AndroidRuntime(1333):     at java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1671)
07-10 13:07:57.777: E/AndroidRuntime(1333):     at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1517)
07-10 13:07:57.777: E/AndroidRuntime(1333):     at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1481)
07-10 13:07:57.777: E/AndroidRuntime(1333):     at android.os.Parcel.writeSerializable(Parcel.java:1274)
07-10 13:07:57.777: E/AndroidRuntime(1333):     ... 25 more

当我尝试启动第二个活动时应用程序崩溃(在线 v.getContext.startActivity ....)。有任何想法吗?

4

2 回答 2

5

您的 CustomObj 还应该实现可序列化:

引起:java.io.NotSerializableException: com.CustomObj

如果 CustomObj 再次包含其他对象,请确保这些对象也实现可序列化。您不必寻找像 String 或 Double 这样的简单类型,因为它们实现了可序列化。

于 2013-07-10T13:30:54.887 回答
1

使用 java.io.Serializable 创建一个可序列化对象

private Class CustomObject implements Serializable
{
   ...
}

Intents 已经使用 put extra 方法发送了可序列化的数据。带有可序列化有效负载的第一个活动转换如下所示:

CustomObject obj = new CustomObject();  

Intent i = new Intent(ThisClass.this, ThatOtherActivity.class);
i.putExtra("foobar", obj);  
startActivity(i);  

接收活动看起来像这样。

Intent i = getIntent();
CustomObject obj = (CustomObject) i.getSerializableObject("foobar");

现在记住了。如果 CustomObject 包含其他自定义对象。确保这些自定义对象也实现了 Serializable。

于 2014-08-14T16:45:21.503 回答