我正在尝试序列化和反序列化实现 Serializable 的自定义 ArrayAdapter。序列化似乎有效,但反序列化在下面标记为“**”的行上给了我一个 IllegalAccessException:
序列化:
FileOutputStream fos = this.openFileOutput ( fileName, Context.MODE_PRIVATE );
ObjectOutputStream os = new ObjectOutputStream ( fos );
os.writeObject ( adapter );
os.close ();
反序列化:
protected SerializableArrayAdapter createAdapter () {
String fileName = "testFile";
try {
FileInputStream fis = this.openFileInput ( fileName );
ObjectInputStream ois = new ObjectInputStream ( fis );
**SerializableArrayAdapter adapter = ( SerializableArrayAdapter ) ois.readObject ();**
ois.close ();
return adapter;
} catch ( Exception ex ) {
ex.printStackTrace ();
}
adapter = new SerializableArrayAdapter ( this, android.R.layout.simple_list_item_1 );
return adapter;
}
我究竟做错了什么?(我是java和android的新手)
编辑:添加了 logcat。嗯,也许有办法让它不那么……乱七八糟?
10-31 16:13:49.960: W/System.err(28966): java.io.InvalidClassException: android.widget.ArrayAdapter; IllegalAccessException
10-31 16:13:49.960: W/System.err(28966): at java.io.ObjectStreamClass.resolveConstructorClass(ObjectStreamClass.java:692)
10-31 16:13:49.960: W/System.err(28966): at java.io.ObjectStreamClass.newInstance(ObjectStreamClass.java:653)
10-31 16:13:49.960: W/System.err(28966): at java.io.ObjectInputStream.readNewObject(ObjectInputStream.java:1819)
10-31 16:13:49.960: W/System.err(28966): at java.io.ObjectInputStream.readNonPrimitiveContent(ObjectInputStream.java:787)
10-31 16:13:49.960: W/System.err(28966): at java.io.ObjectInputStream.readObject(ObjectInputStream.java:2006)
10-31 16:13:49.968: W/System.err(28966): at java.io.ObjectInputStream.readObject(ObjectInputStream.java:1963)
10-31 16:13:49.968: W/System.err(28966): at com.example.tm2.MainActivity.createAdapter(MainActivity.java:41)
10-31 16:13:49.968: W/System.err(28966): at com.example.tm2.MainActivity.onCreate(MainActivity.java:29)
10-31 16:13:49.968: W/System.err(28966): at android.app.Activity.performCreate(Activity.java:5133)
10-31 16:13:49.968: W/System.err(28966): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
10-31 16:13:49.968: W/System.err(28966): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
10-31 16:13:49.968: W/System.err(28966): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
10-31 16:13:49.968: W/System.err(28966): at android.app.ActivityThread.access$600(ActivityThread.java:141)
10-31 16:13:49.968: W/System.err(28966): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
10-31 16:13:49.968: W/System.err(28966): at android.os.Handler.dispatchMessage(Handler.java:99)
10-31 16:13:49.968: W/System.err(28966): at android.os.Looper.loop(Looper.java:137)
10-31 16:13:49.968: W/System.err(28966): at android.app.ActivityThread.main(ActivityThread.java:5103)
10-31 16:13:49.968: W/System.err(28966): at java.lang.reflect.Method.invokeNative(Native Method)
10-31 16:13:49.968: W/System.err(28966): at java.lang.reflect.Method.invoke(Method.java:525)
10-31 16:13:49.968: W/System.err(28966): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
10-31 16:13:49.968: W/System.err(28966): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-31 16:13:49.968: W/System.err(28966): at dalvik.system.NativeStart.main(Native Method)