我有以下课程:
public class NightPoint implements Serializable
{
Date time;
int y;
public NightPoint(){};
public NightPoint(Date time, int y)
{
this.time = time;
this.y = y;
}
}
但是当我尝试执行以下操作时,我得到NotSerializableException:
try {
FileOutputStream fos = act.openFileOutput(fileName, Context.MODE_PRIVATE);
ObjectOutputStream os = new ObjectOutputStream(fos);
os.writeObject(np);
os.close();
} catch (IOException e) {
Log.d("Output", "Write: IOException -->" + e.toString());
e.printStackTrace();
}
我已经检查确保Date是可序列化的,所以我无法弄清楚我做错了什么......
10-12 17:10:05.542: W/System.err(7330): java.io.NotSerializableException: com.SPTechnos.sleepsmart.Main
10-12 17:10:05.542: W/System.err(7330): at java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1364)
10-12 17:10:05.542: W/System.err(7330): at java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1671)
10-12 17:10:05.542: W/System.err(7330): at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1517)
10-12 17:10:05.542: W/System.err(7330): at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1481)
10-12 17:10:05.542: W/System.err(7330): at java.io.ObjectOutputStream.writeFieldValues(ObjectOutputStream.java:979)
10-12 17:10:05.542: W/System.err(7330): at java.io.ObjectOutputStream.defaultWriteObject(ObjectOutputStream.java:368)
10-12 17:10:05.542: W/System.err(7330): at java.io.ObjectOutputStream.writeHierarchy(ObjectOutputStream.java:1074)
10-12 17:10:05.542: W/System.err(7330): at java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1404)
10-12 17:10:05.542: W/System.err(7330): at java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1671)
10-12 17:10:05.542: W/System.err(7330): at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1517)
10-12 17:10:05.542: W/System.err(7330): at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1481)
10-12 17:10:05.542: W/System.err(7330): at com.SPTechnos.sleepsmart.FileLogger.writeNP(FileLogger.java:34)
10-12 17:10:05.542: W/System.err(7330): at com.SPTechnos.sleepsmart.Main.onCreate(Main.java:50)
10-12 17:10:05.542: W/System.err(7330): at android.app.Activity.performCreate(Activity.java:5451)
10-12 17:10:05.542: W/System.err(7330): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093)
10-12 17:10:05.542: W/System.err(7330): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
10-12 17:10:05.542: W/System.err(7330): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2392)
10-12 17:10:05.542: W/System.err(7330): at android.app.ActivityThread.access$900(ActivityThread.java:169)
10-12 17:10:05.552: W/System.err(7330): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1280)
10-12 17:10:05.552: W/System.err(7330): at android.os.Handler.dispatchMessage(Handler.java:102)
10-12 17:10:05.552: W/System.err(7330): at android.os.Looper.loop(Looper.java:146)
10-12 17:10:05.552: W/System.err(7330): at android.app.ActivityThread.main(ActivityThread.java:5487)
10-12 17:10:05.552: W/System.err(7330): at java.lang.reflect.Method.invokeNative(Native Method)
10-12 17:10:05.552: W/System.err(7330): at java.lang.reflect.Method.invoke(Method.java:515)
10-12 17:10:05.552: W/System.err(7330): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
10-12 17:10:05.552: W/System.err(7330): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
10-12 17:10:05.552: W/System.err(7330): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:0)
出于某种原因,这个问题源于我创造了课程" NightPoint"在课堂内" Main&#34 ;;但是,只需将其移动到唯一的文件即可解决问题。