0

I am trying to add a custom array to shared preferences in android.

I have a custom arrayList that contains set data (Strings and NodeList), the data is stored in an array with the following name ArrayList<XMLDataArrayStructure> menuItems = new ArrayList<XMLDataArrayStructure>(); I have found a solution that says I should use an "ObjectSerializer" class to store the data. Everything works fine up until a point, I get the following error

W/System.err(32052): java.io.NotSerializableException: package.XMLDataArrayStructure
W/System.err(32052):    at java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1364)
W/System.err(32052):    at java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1671)
W/System.err(32052):    at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1517)
W/System.err(32052):    at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1481)
W/System.err(32052):    at java.util.ArrayList.writeObject(ArrayList.java:644)
W/System.err(32052):    at java.lang.reflect.Method.invokeNative(Native Method)
W/System.err(32052):    at java.lang.reflect.Method.invoke(Method.java:511)
W/System.err(32052):    at java.io.ObjectOutputStream.writeHierarchy(ObjectOutputStream.java:1053)
W/System.err(32052):    at java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1404)
W/System.err(32052):    at java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1671)
W/System.err(32052):    at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1517)
W/System.err(32052):    at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1481)
W/System.err(32052):    at package.ObjectSerializer.serialize(ObjectSerializer.java:17)
W/System.err(32052):    at package.GatherXMLData.getData(GatherXMLData.java:94)
W/System.err(32052):    at package.Splash$gatherData.doInBackground(Splash.java:63)
W/System.err(32052):    at package.Splash$gatherData.doInBackground(Splash.java:1)
W/System.err(32052):    at android.os.AsyncTask$2.call(AsyncTask.java:287)
W/System.err(32052):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
W/System.err(32052):    at java.util.concurrent.FutureTask.run(FutureTask.java:137)
W/System.err(32052):    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
W/System.err(32052):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
W/System.err(32052):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
W/System.err(32052):    at java.lang.Thread.run(Thread.java:864)

It is failing on the following

public static String serialize(Serializable obj) throws IOException {
    if (obj == null)
        return "";
    ByteArrayOutputStream serialObj = new ByteArrayOutputStream();
    ObjectOutputStream objStream = new ObjectOutputStream(serialObj);
    objStream.writeObject(obj);
    objStream.close();
    return encodeBytes(serialObj.toByteArray());
}

Can someone help please, this would really help me a lot as I would only have to make one network call then.

Thanks

EDIT

Here is the code from the XMLDataArrayStructure class, it does implement serializable but I am getting the following error still

CODE

public class XMLDataArrayStructure implements Serializable{

    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    String title, date, start_time, end_time, venue, image, sponsor_logo;
    NodeList people;
    Spanned description;

    public void setPeople(NodeList people) {
        this.people = people;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public void setDate(String date) {
        this.date = date;
    }

    public void setStartTime(String start_time) {
        this.start_time = start_time;
    }

    public void setEndTime(String end_time) {
        this.end_time = end_time;
    }

    public void setVenue(String venue) {
        this.venue = venue;
    }

    public void setDescription(Spanned spanned) {
        this.description = spanned;
    }

    public void setImage(String image) {
        this.image = image;
    }

    public void setSponsorLogo(String sponsor_logo) {
        this.sponsor_logo = sponsor_logo;
    }

    public NodeList getPeople() {
        return people;
    }

    public String getTitle() {
        return title;
    }

    public String getDate() {
        return date;
    }

    public String getStartTime() {
        return start_time;
    }

    public String getEndTime() {
        return end_time;
    }

    public String getVenue() {
        return venue;
    }

    public Spanned getDescription() {
        return description;
    }

    public String getImage() {
        return image;
    }

    public String getSponsorLogo() {
        return sponsor_logo;
    }

}

ERROR

W/System.err(1380): java.io.NotSerializableException: android.text.SpannableStringBuilder
W/System.err(1380):     at java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1364)
W/System.err(1380):     at java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1671)
W/System.err(1380):     at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1517)
W/System.err(1380):     at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1481)
W/System.err(1380):     at java.io.ObjectOutputStream.writeFieldValues(ObjectOutputStream.java:979)
W/System.err(1380):     at java.io.ObjectOutputStream.defaultWriteObject(ObjectOutputStream.java:368)
W/System.err(1380):     at java.io.ObjectOutputStream.writeHierarchy(ObjectOutputStream.java:1074)
W/System.err(1380):     at java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1404)
W/System.err(1380):     at java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1671)
W/System.err(1380):     at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1517)
W/System.err(1380):     at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1481)
W/System.err(1380):     at java.util.ArrayList.writeObject(ArrayList.java:644)
W/System.err(1380):     at java.lang.reflect.Method.invokeNative(Native Method)
W/System.err(1380):     at java.lang.reflect.Method.invoke(Method.java:511)
W/System.err(1380):     at java.io.ObjectOutputStream.writeHierarchy(ObjectOutputStream.java:1053)
W/System.err(1380):     at java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1404)
W/System.err(1380):     at java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1671)
W/System.err(1380):     at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1517)
W/System.err(1380):     at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1481)
W/System.err(1380):     at package.ObjectSerializer.serialize(ObjectSerializer.java:17)
W/System.err(1380):     at package.GatherXMLData.getData(GatherXMLData.java:89)
W/System.err(1380):     at package.Splash$gatherData.doInBackground(Splash.java:63)
W/System.err(1380):     at package.Splash$gatherData.doInBackground(Splash.java:1)
W/System.err(1380):     at android.os.AsyncTask$2.call(AsyncTask.java:287)
W/System.err(1380):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
W/System.err(1380):     at java.util.concurrent.FutureTask.run(FutureTask.java:137)
W/System.err(1380):     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
W/System.err(1380):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
W/System.err(1380):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
W/System.err(1380):     at java.lang.Thread.run(Thread.java:864)
4

2 回答 2

1
Class XMLDataArrayStructure implements Serializable
于 2013-07-12T09:58:29.853 回答
0

很可能,您的课程XMLDataArrayStructure是非Serializable. 可能有两个原因:

  1. 你的类本身没有实现Serializable.

  2. 即使类实现了Serializable接口,也包含引用非Serializable对象的语句。

如果可以,请发布课程代码。

于 2013-07-12T09:56:59.937 回答