通过反序列化恢复ArrayAdapter?

时间:2013-10-31 14:25:24

标签: android serialization deserialization android-arrayadapter

我正在尝试序列化和反序列化实现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)

1 个答案:

答案 0 :(得分:0)

您的代码在哪一行引发异常?

看看这个: 保存:

FileOutputStream fos = context.openFileOutput(fileName, Context.MODE_PRIVATE);
ObjectOutputStream os = new ObjectOutputStream(fos);
os.writeObject(your object);
os.close();

负载:

FileInputStream fis = context.openFileInput(fileName);
ObjectInputStream is = new ObjectInputStream(fis);
YourClass yourObject = (YourClass) is.readObject();
is.close();
return yourObject;

这可以让你回到正轨