【问题标题】:Solve NotSerializableException解决 NotSerializableException
【发布时间】:2012-08-23 12:48:08
【问题描述】:

我对 Java 还很陌生,我似乎无法解决收到的 NotSerializableException 问题。我正在向用户的 sdcard 写入一个对象,接下来我需要读回它。

这是我的代码:

@Override
protected void createAndAttachView(int id, FrameLayout frame) {

    //Read the panel from the memory
    PanelWrapper pnl = (PanelWrapper) readObjectFromMemory(B4AHelper.getDirDefaultExternal(), "layout.frame");

    //Add the view
    frame.addView(pnl.getObject());

    //Broadcast an intent to the user.
    intent = new IntentWrapper();
    intent.Initialize("createAndAttachView", "");
    intent.PutExtra("id", id);
    intent.PutExtra("message", "View Attached");
    sendBroadcast(intent.getObject());

    //Log - debugging
    Log.i("B4A", "View attached!");
}

我正在使用来自herereadObjectFromMemorywriteObjectFromMemory,这是错误:

Error Message: anywheresoftware.b4a.BALayout


Error Message: Read an exception; java.io.NotSerializableException: anywheresoftware.b4a.BALayout

Caused by: java.lang.NullPointerException
    at com.rootsoft.standout.MostBasicWindow.createAndAttachView(MostBasicWindow.java:53)
    at com.rootsoft.standout.StandOutWindow$Window.<init>(StandOutWindow.java:2213)
    at com.rootsoft.standout.StandOutWindow.show(StandOutWindow.java:1416)
    at com.rootsoft.standout.StandOutWindow.onStartCommand(StandOutWindow.java:716)
    at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2359)
    ... 10 more

我想知道是否有可能解释得尽可能好,因为我是新手,这也可能对其他人有所帮助。

【问题讨论】:

标签: android


【解决方案1】:

在这里,您可以阅读有关 java 中常见的序列化的信息。

http://java.sun.com/developer/technicalArticles/Programming/serialization/

简而言之,可序列化对象有一个名为serialVersionUIDstatic long 字段,该字段由类的成员字段和方法生成。它用于检查类的定义是否匹配。

例如,您有一个Serializeable 对象,您将其写入文件为byte[],并且您想稍后再读回它。如果同时您更改了类中的某些字段或方法(例如,文件中的类定义和当前使用的类定义不再相同),那么 java 可以从无效的 UID 中发现这些差异并且它会知道,虽然它们可能是同一类型,但它们并不兼容。

当然,您也可以分配一个默认 UID,或者如果您知道这种情况肯定不会发生,您可以将其留空。

至于摆脱你的异常,你只需要在你的类中实现Serializable 接口(在你的情况下是anywheresoftware.b4a.BALayout)。

【讨论】:

  • 太好了,谢谢。如果您无法访问该课程怎么办?例如它被打包在一个库中?
猜你喜欢
  • 2011-05-13
  • 1970-01-01
  • 1970-01-01
  • 2013-03-12
  • 1970-01-01
  • 2015-06-01
  • 2013-05-14
  • 2023-04-03
  • 2014-07-23
相关资源
最近更新 更多