Parcelable遇到IOException写入可序列化的对象getactivity()

所以我得到这个logcat:

java.lang.RuntimeException: Parcelable encountered IOException writing serializable object (name = com.resources.student_list.Student) 

我知道这意味着我的学生课不是可序列化的,但是,这是我的学生课:

 import java.io.Serializable; public class Student implements Comparable<Student>, Serializable{ private static final long serialVersionUID = 1L; private String firstName, lastName; private DSLL<Grade> gradeList; public Student() { firstName = ""; lastName = ""; gradeList = new DSLL<Grade>(); } public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } public DSLL<Grade> getGradeList() { return gradeList; } public void setGradeList(DSLL<Grade> gradeList) { this.gradeList = gradeList; } public int compareTo(Student arg0) { return this.lastName.compareTo(arg0.getLastName()); } } 

这是使用getIntent()方法的代码:

 public void onItemClick(AdapterView<?> parent, View viewClicked, int pos, long id) { Student clickedStudent = studentList.get(pos); int position = pos; Intent intent = new Intent(getActivity().getApplicationContext(), ShowStudentActivity.class); Log.e("CINTENT","CREATED!!!"); intent.putExtra("clickedStudent",clickedStudent); intent.putExtra("newStudentList",newStudentList); intent.putExtra("position",position); Log.e("putExtra","Passed"); Log.e("Start activity","passed"); startActivity(intent); } }); 

请帮我弄清楚这是什么错误。

这里是整个LogCat:

 04-17 16:12:28.890: E/AndroidRuntime(22815): FATAL EXCEPTION: main 04-17 16:12:28.890: E/AndroidRuntime(22815): java.lang.RuntimeException: Parcelable encountered IOException writing serializable object (name = com.resources.student_list.Student) 04-17 16:12:28.890: E/AndroidRuntime(22815): at android.os.Parcel.writeSerializable(Parcel.java:1181) 04-17 16:12:28.890: E/AndroidRuntime(22815): at android.os.Parcel.writeValue(Parcel.java:1135) 04-17 16:12:28.890: E/AndroidRuntime(22815): at android.os.Parcel.writeMapInternal(Parcel.java:493) 04-17 16:12:28.890: E/AndroidRuntime(22815): at android.os.Bundle.writeToParcel(Bundle.java:1612) 04-17 16:12:28.890: E/AndroidRuntime(22815): at android.os.Parcel.writeBundle(Parcel.java:507) 04-17 16:12:28.890: E/AndroidRuntime(22815): at android.content.Intent.writeToParcel(Intent.java:6111) 04-17 16:12:28.890: E/AndroidRuntime(22815): at android.app.ActivityManagerProxy.startActivity(ActivityManagerNative.java:1613) 04-17 16:12:28.890: E/AndroidRuntime(22815): at android.app.Instrumentation.execStartActivity(Instrumentation.java:1422) 04-17 16:12:28.890: E/AndroidRuntime(22815): at android.app.Activity.startActivityForResult(Activity.java:3191) 04-17 16:12:28.890: E/AndroidRuntime(22815): at android.support.v4.app.FragmentActivity.startActivityFromFragment(FragmentActivity.java:848) 04-17 16:12:28.890: E/AndroidRuntime(22815): at android.support.v4.app.Fragment.startActivity(Fragment.java:878) 04-17 16:12:28.890: E/AndroidRuntime(22815): at com.example.student_lists.MainActivity$DummySectionFragment$2.onItemClick(MainActivity.java:477) 04-17 16:12:28.890: E/AndroidRuntime(22815): at android.widget.AdapterView.performItemClick(AdapterView.java:292) 04-17 16:12:28.890: E/AndroidRuntime(22815): at android.widget.AbsListView.performItemClick(AbsListView.java:1058) 04-17 16:12:28.890: E/AndroidRuntime(22815): at android.widget.AbsListView$PerformClick.run(AbsListView.java:2514) 04-17 16:12:28.890: E/AndroidRuntime(22815): at android.widget.AbsListView$1.run(AbsListView.java:3168) 04-17 16:12:28.890: E/AndroidRuntime(22815): at android.os.Handler.handleCallback(Handler.java:605) 04-17 16:12:28.890: E/AndroidRuntime(22815): at android.os.Handler.dispatchMessage(Handler.java:92) 04-17 16:12:28.890: E/AndroidRuntime(22815): at android.os.Looper.loop(Looper.java:137) 04-17 16:12:28.890: E/AndroidRuntime(22815): at android.app.ActivityThread.main(ActivityThread.java:4447) 04-17 16:12:28.890: E/AndroidRuntime(22815): at java.lang.reflect.Method.invokeNative(Native Method) 04-17 16:12:28.890: E/AndroidRuntime(22815): at java.lang.reflect.Method.invoke(Method.java:511) 04-17 16:12:28.890: E/AndroidRuntime(22815): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) 04-17 16:12:28.890: E/AndroidRuntime(22815): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 04-17 16:12:28.890: E/AndroidRuntime(22815): at dalvik.system.NativeStart.main(Native Method) 04-17 16:12:28.890: E/AndroidRuntime(22815): Caused by: java.io.NotSerializableException: com.resources.student_list.DSLL$DNode 04-17 16:12:28.890: E/AndroidRuntime(22815): at java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1364) 04-17 16:12:28.890: E/AndroidRuntime(22815): at java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1671) 04-17 16:12:28.890: E/AndroidRuntime(22815): at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1517) 04-17 16:12:28.890: E/AndroidRuntime(22815): at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1481) 04-17 16:12:28.890: E/AndroidRuntime(22815): at java.io.ObjectOutputStream.writeFieldValues(ObjectOutputStream.java:979) 04-17 16:12:28.890: E/AndroidRuntime(22815): at java.io.ObjectOutputStream.defaultWriteObject(ObjectOutputStream.java:368) 04-17 16:12:28.890: E/AndroidRuntime(22815): at java.io.ObjectOutputStream.writeHierarchy(ObjectOutputStream.java:1074) 04-17 16:12:28.890: E/AndroidRuntime(22815): at java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1404) 04-17 16:12:28.890: E/AndroidRuntime(22815): at java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1671) 04-17 16:12:28.890: E/AndroidRuntime(22815): at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1517) 04-17 16:12:28.890: E/AndroidRuntime(22815): at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1481) 04-17 16:12:28.890: E/AndroidRuntime(22815): at java.io.ObjectOutputStream.writeFieldValues(ObjectOutputStream.java:979) 04-17 16:12:28.890: E/AndroidRuntime(22815): at java.io.ObjectOutputStream.defaultWriteObject(ObjectOutputStream.java:368) 04-17 16:12:28.890: E/AndroidRuntime(22815): at java.io.ObjectOutputStream.writeHierarchy(ObjectOutputStream.java:1074) 04-17 16:12:28.890: E/AndroidRuntime(22815): at java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1404) 04-17 16:12:28.890: E/AndroidRuntime(22815): at java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1671) 04-17 16:12:28.890: E/AndroidRuntime(22815): at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1517) 04-17 16:12:28.890: E/AndroidRuntime(22815): at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1481) 04-17 16:12:28.890: E/AndroidRuntime(22815): at android.os.Parcel.writeSerializable(Parcel.java:1176) 
 Caused by: java.io.NotSerializableException: com.resources.student_list.DSLL$DNode 

您的DSLL类似乎有一个DNode静态内部类,并且DNode不是可Serializable

如果你不能使DNode可序列化,一个好的解决scheme是将“瞬态”添加到variables。

例:

 public static transient DNode dNode = null; 

当使用Intent.putExtra(…)时,这将忽略variables。

当您的自定义类具有其他类的属性,例如“位图”时,会出现问题。 我所做的是将属性字段从“私人位图照片”更改为“私人瞬间位图照片”。 然而,我的getIntent()在接收器活动后图像是空的。 正因为如此,我将自定义类传递给intent,并且从图像中创build了一个字节数组,并将其单独传递给intent:

selectedItem是我的自定义对象,getPlacePhoto是他获取图像的方法。 我已经设置它,现在我只是先把它转换,并将其分开传递:

  Bitmap image = selectedItem.getPlacePhoto(); image.compress(Bitmap.CompressFormat.PNG, 100, stream); byte[] byteArray = stream.toByteArray(); Intent intent = new Intent(YourPresentActivity.this, TheReceiverActivity.class); intent.putExtra("selectedItem", selectedItem); intent.putExtra("image", byteArray); startActivity(intent); 

`

然后在接收器的活动,我得到我的对象和图像作为字节数组,解码图像,并将其设置为我的对象作为照片属性。

  Intent intent = getIntent(); selectedItem = (ListItem) intent.getSerializableExtra("selectedItem"); byte[] byteArray = getIntent().getByteArrayExtra("image"); Bitmap image = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length); selectedItem.setPhoto(image); 

我也是阶段这些错误,我在modelClass中的一点点变化, 这是实现Serializable接口,如:

在那个Model类中,还用writeToParcel()覆盖方法实现了Parcelable 接口

然后刚刚得到“创build创build者”的错误,所以CREATOR是写和创build与模型类构造参数&不带参数 ..

  @Override public void writeToParcel(Parcel dest, int flags) { dest.writeString(id); dest.writeString(name); } protected ArtistTrackClass(Parcel in) { id = in.readString(); name = in.readString(); } public ArtistTrackClass() { } public static final Creator<ArtistTrackClass> CREATOR = new Creator<ArtistTrackClass>() { @Override public ArtistTrackClass createFromParcel(Parcel in) { return new ArtistTrackClass(in); } @Override public ArtistTrackClass[] newArray(int size) { return new ArtistTrackClass[size]; } }; 

这里,

ArtistTrackClass – > ModelClass

带有Parcel参数的构造函数“读取我们的属性” ,writeToParcel()是“写入我们的属性”

Grade类也必须实现Serializable

 public class Grade implements Serializable { .....your content.... } 

你的OneThread类也应该实现Serializable。 所有的子类内部子类都必须实现Serializable

这是为我工作…