所以我在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)
答案 0 :(得分:257)
Caused by: java.io.NotSerializableException: com.resources.student_list.DSLL$DNode
您的DSLL
课程似乎有DNode
个静态内部课程,DNode
不是Serializable
。
答案 1 :(得分:36)
您的OneThread类也应该实现Serializable。所有 子类 和 内部子类 必须实现Serializable 。
这对我有用......
答案 2 :(得分:23)
如果你不能使DNode序列化,一个好的解决方案就是在变量中增加“瞬态”。
示例:
public static transient DNode dNode = null;
这将在使用Intent.putExtra(...)时忽略该变量。
答案 3 :(得分:3)
对我来说,这是通过使变量具有类瞬态来解决的。
之前的代码:
public class UserLocation implements Serializable {
public Location lastKnownLocation;
public UserLocation() {}
}
之后的代码
public class UserLocation implements Serializable {
public transient Location lastKnownLocation;
public UserLocation() {}
}
答案 4 :(得分:3)
如果您的POJO内部包含任何其他模型,也应该实现Serializable
答案 5 :(得分:2)
如果您的自定义类具有某些其他类的属性,则会出现问题,例如&#34;位图&#34 ;.我做的是从&#34;私人位图照片&#34;更改属性字段。到&#34;私人瞬态位图照片&#34;。但是,在接收器活动中的getIntent()之后,图像为空。因此,我将自定义类传递给了intent,并且我还从图像中创建了一个字节数组,并将其分别传递给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);
答案 6 :(得分:0)
我也是这些错误的阶段,我在 modelClass中做了一点改变,它实现了Serializable接口,如:
该Model类还使用 writeToParcel()覆盖方法实现 Parcelable接口
然后只是错误&#34;创建创建者&#34; 所以 CREATOR 是写的,并且还使用 modelclass构造函数创建带参数&amp;没有参数 ..
@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 - &gt; ModelClass 强>
包含参数的构造函数&#34;读取我们的属性&#34; 和writeToParcel()是&#34;写下我们的属性&#34;
答案 7 :(得分:0)
Grade类还必须实现Serializable
public class Grade implements Serializable {
.....your content....
}
答案 8 :(得分:0)
需要将所有的数组列表更改为Bean类中的Serializable wif:
public static class PremiumListBean implements Serializable {
private List<AddOnValueBean> AddOnValue;
public List<AddOnValueBean> getAddOnValue() {
return AddOnValue;
}
public void setAddOnValue(List<AddOnValueBean> AddOnValue) {
this.AddOnValue = AddOnValue;
}
public static class AddOnValueBean implements Serializable{
@SerializedName("Premium")
private String Premium;
public String getPremium() {
return Premium;
}
public void setPremium(String Premium) {
this.Premium = Premium;
}
}
}
答案 9 :(得分:0)
发生异常是由于任何内部类或其他引用的类均未实现可序列化的事实。因此,使所有引用的类都实现可序列化。
答案 10 :(得分:0)
我遇到同样的问题,问题是存在一些带有static关键字的内部类。删除static关键字后它开始工作,并且内部类也应实现为Serializable
class A implements Serializable{
class static B{
}
}
class A implements Serializable{
class B implements Serializable{
}
}
答案 11 :(得分:0)
就我而言,我也必须将MainActivity
实施为Serializable
。原因我需要从MainActivity
启动服务:
public class MainActivity extends AppCompatActivity implements Serializable {
...
musicCover = new MusicCover(); // A Serializable Object
...
sIntent = new Intent(MainActivity.this, MusicPlayerService.class);
sIntent.setAction(MusicPlayerService.ACTION_INITIALIZE_COVER);
sIntent.putExtra(MusicPlayerService.EXTRA_COVER, musicCover);
startService(sIntent);
}