将自定义数组添加到android中的共享首选项

时间:2013-07-12 09:51:20

标签: android arraylist sharedpreferences

我正在尝试将自定义数组添加到android中的共享首选项。

我有一个包含set data(Strings和NodeList)的自定义arrayList,数据存储在一个名为ArrayList<XMLDataArrayStructure> menuItems = new ArrayList<XMLDataArrayStructure>();的数组中 我找到了一个解决方案,说我应该使用“ ObjectSerializer ”类来存储数据。一切正常,直到某一点,我得到以下错误

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)

以下

失败了
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());
}

有人可以帮助,这对我来说真的很有帮助,因为我只需要拨打一个网络电话。

由于

修改

这是来自XMLDataArrayStructure类的代码,它确实实现了serializable,但我仍然收到以下错误

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;
    }

}

错误

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)

2 个答案:

答案 0 :(得分:1)

Class XMLDataArrayStructure implements Serializable

答案 1 :(得分:0)

您的班级XMLDataArrayStructure很可能是非Serializable。原因有两个:

  1. 您的课程本身并未实施Serializable

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

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