【问题标题】:How to write custom object to file in Android如何在Android中将自定义对象写入文件
【发布时间】:2013-08-20 12:03:47
【问题描述】:
private Map<String, List<DrawPath>> savedPath= new LinkedHashMap<String, List<DrawPath>>();

我想将此“savedPath”写入文件,但抛出 IOException:

public static class DrawPath implements Serializable{
        /**
         * 
         */
        public static final long serialVersionUID = 1L;
        public Path path;
                public Paint paint;
      }

 ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(sdFile));

out.writeObject(savedPath); // throws IOExcption

如果更改为Map&lt;String, String&gt;,它会成功运行,它与我的自定义 DrawPath 类有关吗?如何将此对象写入文件成功?谢谢

【问题讨论】:

  • 请将您的 Logcat 错误日志连同您的问题一起发布。
  • 它被 try { out.writeObject(savedPath); 捕获} catch (IOException e) { // TODO 自动生成的 catch 块 e.printStackTrace(); }
  • 嗯,控制台选项卡附近有一个logcat选项卡。当应用程序崩溃时,您捕获的错误会打印在那里。请在应用程序崩溃时找到您的 logcat 中的所有错误代码和字符串,并将其粘贴到您的问题中。
  • 你知道我应该保存画图吗?或者除了 DrawPath 类之外的其他对象如何 Serializable ?

标签: android file object serializable objectoutputstream


【解决方案1】:

确保您写入文件的所有对象都是可序列化的,保存 Paint 可能是问题

【讨论】:

  • 我应该如何保存画图?或者除了 DrawPath 类之外的其他对象如何序列化?非常感谢!
猜你喜欢
  • 2014-09-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-15
  • 2011-01-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多