所以我有一个类,有一个实现可序列化的图像:
public class Contact implements Serializable
{
...
public static final String CONTACT_KEY = "contactKey";
private transient Bitmap mImage;
...
}
我将它传递给一个片段:
Bundle bundle = new Bundle();
final FragmentManager fragmentManager = getFragmentManager();
// Load the fragment
Fragment contactFragment = new ContactFragment();
contactFragment.setArguments(bundle);
fragmentManager.beginTransaction()
.replace(R.id.contactContainer, headerFragment, "ContactFragment")
.commit();
并拉出图像(成功)并使用它:
public class ContactFragment extends Fragment
{
private static final String TAG = "ContactFragment";
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
Bundle arguments = getArguments();
if (arguments != null)
{
Contact contact = (Contact) arguments.getSerializable(Contact.CONTACT_KEY);
ViewGroup view = (ViewGroup)inflater.inflate(R.layout.contact, container, false);
if (view != null && contact != null)
{
ImageView imageView = (ImageView) view.findViewById(R.id.contactImage);
if (imageView != null)
{
if (contact.hasImage())
{
imageView.setImageBitmap(contact.image());
}
}
}
}
}
}
但是当我后台我的应用程序时,我收到来自系统的运行时异常:
Process: my.package, PID: 11680
java.lang.RuntimeException: Parcelable encountered IOException writing serializable object (name = my.package.Contact)
at android.os.Parcel.writeSerializable(Parcel.java:1316)
at android.os.Parcel.writeValue(Parcel.java:1264)
at android.os.Parcel.writeArrayMapInternal(Parcel.java:618)
at android.os.Bundle.writeToParcel(Bundle.java:1692)
at android.os.Parcel.writeBundle(Parcel.java:636)
at android.app.FragmentState.writeToParcel(Fragment.java:132)
at android.os.Parcel.writeTypedArray(Parcel.java:1133)
at android.app.FragmentManagerState.writeToParcel(FragmentManager.java:373)
at android.os.Parcel.writeParcelable(Parcel.java:1285)
at android.os.Parcel.writeValue(Parcel.java:1204)
at android.os.Parcel.writeArrayMapInternal(Parcel.java:618)
at android.os.Bundle.writeToParcel(Bundle.java:1692)
at android.os.Parcel.writeBundle(Parcel.java:636)
at android.app.ActivityManagerProxy.activityStopped(ActivityManagerNative.java:2467)
at android.app.ActivityThread$StopInfo.run(ActivityThread.java:3084)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
Caused by: java.io.NotSerializableException: android.graphics.Bitmap
at java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1364)
at java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1671)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1517)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1481)
at java.io.ObjectOutputStream.writeFieldValues(ObjectOutputStream.java:979)
at java.io.ObjectOutputStream.defaultWriteObject(ObjectOutputStream.java:368)
at java.io.ObjectOutputStream.writeHierarchy(ObjectOutputStream.java:1074)
at java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1404)
at java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1671)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1517)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1481)
at android.os.Parcel.writeSerializable(Parcel.java:1311)
at android.os.Parcel.writeValue(Parcel.java:1264)
at android.os.Parcel.writeArrayMapInternal(Parcel.java:618)
at android.os.Bundle.writeToParcel(Bundle.java:1692)
at android.os.Parcel.writeBundle(Parcel.java:636)
at android.app.FragmentState.writeToParcel(Fragment.java:132)
at android.os.Parcel.writeTypedArray(Parcel.java:1133)
at android.app.FragmentManagerState.writeToParcel(FragmentManager.java:373)
at android.os.Parcel.writeParcelable(Parcel.java:1285)
at android.os.Parcel.writeValue(Parcel.java:1204)
at android.os.Parcel.writeArrayMapInternal(Parcel.java:618)
at android.os.Bundle.writeToParcel(Bundle.java:1692)
at android.os.Parcel.writeBundle(Parcel.java:636)
at android.app.ActivityManagerProxy.activityStopped(ActivityManagerNative.java:2467)
大概是因为它试图保留我的片段参数。
我知道我不应该尝试像这样序列化位图,但我的问题是为什么会这样?android是否在做某种优化?
在这两种情况下,联系人都是序列化的,那么发生了什么?