序列化android.net.Uri对象

序列化android.net.Uri对象,第1张

概述我想序列化android.net.Uri类型的对象的状态.下面是我的带有writeObject和readObject方法的模型类publicclassReminderObjectimplementsSerializable{privatebooleanisReminderOn;privateintfromHours,toHours;privateintinterval;privateArrayList<CharSequen

我想序列化android.net.Uri类型的对象的状态.

下面是我的带有writeObject和readobject方法的模型类

public class ReminderObject implements Serializable {private boolean isReminderOn;private int fromHours, toHours;private int interval;private ArrayList<CharSequence> daysToRepeat;private Uri tonetoPlay;private AdvanceSettingsObject adv;public ReminderObject(boolean isReminderOn, int fromHours, int toHours,        int interval, ArrayList<CharSequence> daysToRepeat, Uri tonetoPlay,        AdvanceSettingsObject adv) {    super();    this.isReminderOn = isReminderOn;    this.fromHours = fromHours;    this.toHours = toHours;    this.interval = interval;    this.daysToRepeat = daysToRepeat;    this.tonetoPlay = tonetoPlay;    this.adv = adv;} /*getters and setters*/    public voID writeObject(ObjectOutputStream op){    try {        op.defaultWriteObject();        op.writeChars(tonetoPlay.toString());    } catch (IOException e) {        // Todo auto-generated catch block        e.printstacktrace();    }}public voID readobject(ObjectinputStream in){    try{        in.defaultReadobject();        tonetoPlay = Uri.parse(in.readUTF());    }catch(Exception e){    }}}

MainActivity的代码段:

try {        ObjectOutputStream os = new ObjectOutputStream(new     fileOutputStream(Environment.getExternalStorageDirectory().getabsolutePath()+"/ReminderData.txt")    );        os.writeObject(reminder); // Getting above mentioned Exception here        Log.i("TAG","reminder serialized");        ObjectinputStream is = new ObjectinputStream(new fileinputStream(Environment.getExternalStorageDirectory().getabsolutePath()+"/ReminderData.txt"));        ReminderObject reminderRead = (ReminderObject) is.readobject();        if(reminderRead!=null)            Log.i("TAG", "Deserialized Reminder object is : "+reminderRead.toString());        else{            Log.i("TAG", "Null received");        }    } catch(ClassNotFoundException cnf){        cnf.printstacktrace();    } catch (fileNotFoundException e) {             // Todo auto-generated catch block        e.printstacktrace();    } catch (IOException e) {        // Todo auto-generated catch block        e.printstacktrace();    }

面临的例外情况:

03-31 23:47:59.246:W / System.err(12681):java.io.NotSerializableException:androID.net.Uri $HIErarchicalUri
03-31 23:47:59.246:W / System.err(12681):at java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1364)
03-31 23:47:59.246:W / System.err(12681):at java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1671)
03-31 23:47:59.246:W / System.err(12681):at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1517)
03-31 23:47:59.246:W / System.err(12681):at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1481)
03-31 23:47:59.246:W / System.err(12681):at java.io.ObjectOutputStream.writeFIEldValues(ObjectOutputStream.java:979)
03-31 23:47:59.246:W / System.err(12681):at java.io.ObjectOutputStream.defaultWriteObject(ObjectOutputStream.java:368)
03-31 23:47:59.246:W / System.err(12681):at java.io.ObjectOutputStream.writeHIErarchy(ObjectOutputStream.java:1074)
03-31 23:47:59.246:W / System.err(12681):at java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1404)
03-31 23:47:59.246:W / System.err(12681):at java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1671)
03-31 23:47:59.246:W / System.err(12681):at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1517)
03-31 23:47:59.246:W / System.err(12681):at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1481)
03-31 23:47:59.246:W / System.err(12681):at com.navkar.navkarreminder.SetReminderActivity.scheduleReminder(SetReminderActivity.java:595)

请求帮助.

解决方法:

我认为问题是你保存问题的数据成员.

private Uri tonetoPlay是非序列化数据类型,您无法将其序列化.

总结

以上是内存溢出为你收集整理的序列化android.net.Uri对象全部内容,希望文章能够帮你解决序列化android.net.Uri对象所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1116078.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-29
下一篇 2022-05-29

发表评论

登录后才能评论

评论列表(0条)

保存