1

I'm trying to serialize and deserialize a custom ArrayAdapter that implements Serializable. Serialization seems to work, but deserialization gives me an IllegalAccessException on the line marked "**" below:

Serialization:

FileOutputStream fos = this.openFileOutput ( fileName, Context.MODE_PRIVATE );
ObjectOutputStream os = new ObjectOutputStream ( fos );
os.writeObject ( adapter );
os.close ();

Deserialization:

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;
}

What am I doing wrong? (I'm new both to java and android)

Edit: logcat added. Eh, perhaps there's a way to make it less.... messy?

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)
abc32112
  • 2,457
  • 9
  • 37
  • 53

1 Answers1

0

On which line from your code does the exception get thrown?

Have a look at this: Save:

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

Load:

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

This should get you back on track

Marius M
  • 496
  • 9
  • 16
  • Doesn't work. The error is on the line "adapter = ( SerializableArrayAdapter ) ois.readObject ();". Although I just corrected this as per your instruction, adding "SerializableArrayAdapter" in the beginning. – abc32112 Oct 31 '13 at 14:51
  • Could you post the logcat related to the error? It could probably some reflection issue. – Marius M Oct 31 '13 at 14:57
  • Hmm, is there a way of copying only the *text* portion? Gets seriously messy otherwise... – abc32112 Oct 31 '13 at 15:06
  • Filter the logcat to output only stuff from your package and only errors. – Marius M Oct 31 '13 at 15:09
  • Well, I've posted logcat but, eh, it looks pretty messy. – abc32112 Oct 31 '13 at 15:15
  • That's fine, how does the constructor of your SerializableArrayAdapter look like? Is it private? – Marius M Oct 31 '13 at 15:19
  • Nope, all the custom class really does is make ArrayAdapter serializable. Full class: public class SerializableArrayAdapter extends ArrayAdapter implements Serializable { public SerializableArrayAdapter ( Context context, int resource ) { super ( context, resource ); } } – abc32112 Oct 31 '13 at 15:21
  • Make sure your class is public and that you have a public parameterless constructor for SerializableArrayAdapter. – Marius M Oct 31 '13 at 15:21
  • The class is public, but I can't seem to create a parameterless constructor. That gives me: "Implicit super constructor ArrayAdapter() is undefined. Must explicitly invoke another constructor" I guess that's because ArrayAdaptor doesn't have a parameterless constructor (?). – abc32112 Oct 31 '13 at 15:24
  • The problem is that the compile does not know how to create an instance of your adapter class. – Marius M Oct 31 '13 at 15:41