关于Android:Parcelable遇到IOException写入可序列化对象getactivity()

Parcelable encountered IOException writing serializable object getactivity()

所以我在logcat上得到这个:

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

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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()方法的代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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)


1
Caused by: java.io.NotSerializableException: com.resources.student_list.DSLL$DNode

你的DSLL类似乎有一个DNode静态的内部类,DNode不是Serializable类。


你的OneThread类也应该实现可序列化。所有子类和内部子类必须实现可序列化。

这对我有用…


如果不能使dnode序列化,一个好的解决方案是向变量添加"transient"。

例子:

1
public static transient DNode dNode = null;

这将在使用intent.putextra(…)时忽略变量。


对于我来说,这是通过使变量与类一起成为瞬态来解决的。

以前的代码:

1
2
3
4
public class UserLocation implements Serializable {
   public Location lastKnownLocation;
   public UserLocation() {}
}

后码

1
2
3
4
public class UserLocation implements Serializable {
    public transient Location lastKnownLocation;
    public UserLocation() {}
}


当您的自定义类具有某些其他类(如"位图")的for属性时,就会出现问题。我所做的是将属性字段从"private bitmap photo"更改为"private transient bitmap photo"。但是,在接收器活动中获取intent()之后,图像是空的。因此,我将自定义类传递给了intent,还从图像创建了一个字节数组,并将其单独传递给intent:

selecteditem是我的自定义对象,getplacephoto是他获取图像的方法。我以前已经设置过了,现在我只需先获取它,然后转换它并单独传递:

1
2
3
4
5
6
7
8
 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);

`

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

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

如果pojo包含内部的任何其他模型,则该模型还应实现可序列化


需要将bean类中的所有arraylist更改为可序列化wif:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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;
        }
    }
 }

Grade类还必须实现可序列化

1
2
3
public class Grade implements Serializable {
.....your content....
}

我还对这些错误进行了阶段性的处理,并且在modelclass中做了一些小的改动,modelclass实现了可序列化的接口,如:

在该模型类中,还使用writeToParcel()重写方法实现Parcelable接口。

然后在"创建创建者"时出错,因此创建者是写的,也可以用ModelClass Contractor创建,其中包含参数和不带参数。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
       @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

带有包参数"read our attributes"和writeToParcel()的构造函数是"write our attributes"