Parcelable遇到IOException编写可序列化对象 - startActivity

时间:2017-07-26 22:00:58

标签: android parcelable serializable

我正在尝试创建注册系统。我在第一个活动中声明了一次Profile的实例,然后我使用同一个实例传递给另一个活动。我已经将同一个对象传递了4次到其他活动,但是有一个给出了这个错误。

课堂电话:

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();
        }
    });

收到的类:

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");
}

}

我的自定义对象类(我没有把getter和setter放得太大):

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;

}

例外:

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)

2 个答案:

答案 0 :(得分:2)

或者:

  • profilePic

  • 删除Profile
  • 将其更改为某些数据类型,该数据类型可以跨越流程边界传递并且不会太大(例如,资源ID),或

ImageView无法跨进程边界传递,并且每个startActivity()调用都会将Intent从您的进程传递到核心操作系统进程(在这种情况下,返回到您的进程) )。相反,传递一些可以告诉您如何填充ImageView Register30中的其他Activity的内容。

答案 1 :(得分:0)

  

引起:java.io.NotSerializableException:   android.support.v7.widget.AppCompatImageView

ImageView不可序列化。相反,您应该在模型上传递包含给定图像的byteArray,并在onCreate方法上重新创建它。