Luiz Adolfo Luiz Adolfo - 3 years ago 83
Android Question

Parcelable encountered IOException writing serializable object - startActivity

I'm trying to create a registration system. I declare an instance of Profile one time in the first activity, then I use this same instance to pass to another activity. I already passed the same object over 4 times to others activities, but there's one that gives this error.

Class call:

btnNext.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
newUser.setProfilePic(pic);
Intent itt = new Intent(Register20.this, Register30.class);
Bundle bd = new Bundle();
bd.putSerializable("newUser", newUser);
itt.putExtras(bd);
startActivity(itt);
Register20.this.finish();
}
});


Class that receives:

public class Register30 extends AppCompatActivity {

Profile newUser = new Profile();


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register30);
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
newUser = (Profile)getIntent().getSerializableExtra("newUser");
}


}

My custom object class (I didn't put getters and setters to not get too big):

public class Profile implements Serializable{

private String name;
private Integer age;
private String sex;
private String description;
private Calendar dateOfBirth;
private String password;
private String emailRegistration;
private int numberCellRegistration;
private ArrayList<Profile> friends = new ArrayList<>();
private ArrayList<Like> likes = new ArrayList<>();
private ImageView profilePic;


}

Exception:

FATAL EXCEPTION: main
Process: com.chest.chestbrasil.chest, PID: 8880
java.lang.RuntimeException: Parcelable encountered IOException writing serializable object (name = com.chest.chestbrasil.chest.Profile)
at android.os.Parcel.writeSerializable(Parcel.java:1468)
at android.os.Parcel.writeValue(Parcel.java:1416)
at android.os.Parcel.writeArrayMapInternal(Parcel.java:686)
at android.os.BaseBundle.writeToParcelInner(BaseBundle.java:1330)
at android.os.Bundle.writeToParcel(Bundle.java:1079)
at android.os.Parcel.writeBundle(Parcel.java:711)
at android.content.Intent.writeToParcel(Intent.java:7804)
at android.app.ActivityManagerProxy.startActivity(ActivityManagerNative.java:2677)
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1574)
at android.app.Activity.startActivityForResult(Activity.java:3921)
at android.support.v4.app.BaseFragmentActivityJB.startActivityForResult(BaseFragmentActivityJB.java:50)
at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:79)
at android.app.Activity.startActivityForResult(Activity.java:3881)
at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:859)
at android.app.Activity.startActivity(Activity.java:4208)
at android.app.Activity.startActivity(Activity.java:4176)
at com.chest.chestbrasil.chest.Register20$2.onClick(Register20.java:73)
at android.view.View.performClick(View.java:5201)
at android.view.View$PerformClick.run(View.java:21163)
at android.os.Handler.handleCallback(Handler.java:746)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5443)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
Caused by: java.io.NotSerializableException: android.support.v7.widget.AppCompatImageView
at java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1344)
at java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1651)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1497)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1461)
at java.io.ObjectOutputStream.writeFieldValues(ObjectOutputStream.java:959)
at java.io.ObjectOutputStream.defaultWriteObject(ObjectOutputStream.java:360)
at java.io.ObjectOutputStream.writeHierarchy(ObjectOutputStream.java:1054)
at java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1384)
at java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1651)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1497)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1461)
at android.os.Parcel.writeSerializable(Parcel.java:1463)
at android.os.Parcel.writeValue(Parcel.java:1416) 
at android.os.Parcel.writeArrayMapInternal(Parcel.java:686) 
at android.os.BaseBundle.writeToParcelInner(BaseBundle.java:1330) 
at android.os.Bundle.writeToParcel(Bundle.java:1079) 
at android.os.Parcel.writeBundle(Parcel.java:711) 
at android.content.Intent.writeToParcel(Intent.java:7804) 
at android.app.ActivityManagerProxy.startActivity(ActivityManagerNative.java:2677) 
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1574) 
at android.app.Activity.startActivityForResult(Activity.java:3921) 
at android.support.v4.app.BaseFragmentActivityJB.startActivityForResult(BaseFragmentActivityJB.java:50) 
at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:79) 
at android.app.Activity.startActivityForResult(Activity.java:3881) 
at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:859) 
at android.app.Activity.startActivity(Activity.java:4208) 
at android.app.Activity.startActivity(Activity.java:4176) 
at com.chest.chestbrasil.chest.Register20$2.onClick(Register20.java:73) 
at android.view.View.performClick(View.java:5201) 
at android.view.View$PerformClick.run(View.java:21163) 
at android.os.Handler.handleCallback(Handler.java:746) 
at android.os.Handler.dispatchMessage(Handler.java:95) 
at android.os.Looper.loop(Looper.java:148) 
at android.app.ActivityThread.main(ActivityThread.java:5443) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)

Answer Source

Either:

  • Remove profilePic from Profile, or

  • Change it to some data type that can be passed across process boundaries and is not too large (e.g., a resource ID), or

An ImageView cannot be passed across process boundaries, and every startActivity() call passes the Intent from your process to a core OS process (and, in this case, back to your process). Instead, pass something that can tell you how to populate some other ImageView in the Register30 activity.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download