2017-07-26 32 views
0

我正在嘗試創建註冊系統。我在第一個活動中聲明瞭一次配置文件的實例,然後使用相同的實例傳遞給另一個活動。我已經將同一個對象超過4次傳遞給其他人的活動,但是有一個會給出這個錯誤。Parcelable遇到寫入可序列化對象的IOException - startActivity

級呼叫:接收

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

或者:

  • Profile刪除profilePic,或

  • 它更改爲可跨進程邊界被傳遞,並且一些數據類型不太大(例如,資源ID),或者

ImageView不能跨進程邊界傳遞,每一個startActivity()調用傳遞從過程Intent到核心操作系統進程(並且,在這種情況下,回到你的過程)。相反,請傳遞一些信息,告訴您如何填充Register30Activity中的其他一些ImageView

+0

謝謝你的回答。我會尋找一些事情來做到這一點。 – Adolfok3

0

產生的原因:java.io.NotSerializableException: android.support.v7.widget.AppCompatImageView

的ImageView的是不可序列化。相反,您應該將包含給定圖像的byteArray傳遞給模型,並使用onCreate方法重新創建它。

相關問題