2012-08-23 29 views
0

我对Java相当陌生,似乎无法解决我收到的这个NotSerializableException。我正在写一个对象给用户的SD卡,接下来我需要读回它。解决NotSerializableException

这是我的代码:

@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 

我想,如果是有可能解释为好尽可能我是相当新的,这可能也会帮助其他人。

+0

看看这个解决方案。 http://stackoverflow.com/a/4552014/1395259 –

+0

谢谢!但我仍然不知道在哪里必须将“瞬态”放在前面 – Verhelst

回答

2

在这里你可以阅读关于在java中的序列化通用。

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

概括地说,一个序列化的对象具有名为serialVersionUID一个static long字段从成员字段和所述类的方法生成的。它用于检查类的定义是否匹配。

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

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

至于摆脱你的例外,你只需要在你的课上实现Serializable界面(在你的情况下anywheresoftware.b4a.BALayout)。

+0

太好了,谢谢。如果你不能访问该类,该怎么办?例如它被封装在一个库中? – Verhelst