I'm currently working on an Android app that automatically changes the phones state based on a certain event, such as battery level. I have a Profile class which has 3 main parameters, names, Event and State (Event and State are 2 other custom classes)
In my ProfileEdit class, there are various components such as EditText and Spinner for users to configure a Profile. I need access to an ArrayList of type Profile in a ListActivity class which displays each profile, and my ProfileEdit class which gets the current ArrayList and adds the new profile, or updates one.
The idea I had to do this first was to use ObjectOutputStream to write my ArrayList to the phones sd card, then it can be retrieved by any class that needs it. However my attempts to do this so far just end up throwing an IOException. My code is as follows:
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("/sdcard/profile.prof"));
oos.writeObject(profiles);
oos.flush();
oos.close();
The profiles object is my ArrayList to be written to the file. I can't figure out why this is throwing the exception. I tried changing the object to a string by calling writeObject("Hello"); and that no longer threw the exception, so I can only assume its a problem with the ArrayList. I was under the impression that the ArrayList class implements serializable and my Profile class also implements serializable, although it does the same thing without.
Alternatively, if there's a better way I can give both my ProfileList (ListActivity) and ProfileEdit classes access to the same ArrayList then please let me know.
Edit: Here's the IO Exceptions stack trace: (Line 114 of the ProfileEdit class is the writeObject() line)
EZSettings(14788): IO Exception
EZSettings(14788): java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1535)
EZSettings(14788): java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1847)
EZSettings(14788): java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1689)
EZSettings(14788): java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1653)
EZSettings(14788): java.util.ArrayList.writeObject(ArrayList.java:651)
EZSettings(14788): java.lang.reflect.Method.invokeNative(Native Method)
EZSettings(14788): java.lang.reflect.Method.invoke(Method.java:507)
EZSettings(14788): java.io.ObjectOutputStream.writeHierarchy(ObjectOutputStream.java:1219)
EZSettings(14788): java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1575)
EZSettings(14788): java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1847)
EZSettings(14788): java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1689)
EZSettings(14788): java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1653)
EZSettings(14788): com.ezstatechanger.ProfileEdit.saveState(ProfileEdit.java:114)
EZSettings(14788): com.ezstatechanger.ProfileEdit.access$0(ProfileEdit.java:80)
EZSettings(14788): com.ezstatechanger.ProfileEdit$1.onClick(ProfileEdit.java:72)
EZSettings(14788): android.view.View.performClick(View.java:2538)
EZSettings(14788): android.view.View$PerformClick.run(View.java:9152)
EZSettings(14788): android.os.Handler.handleCallback(Handler.java:587)
EZSettings(14788): android.os.Handler.dispatchMessage(Handler.java:92)
EZSettings(14788): android.os.Looper.loop(Looper.java:123)
EZSettings(14788): android.app.ActivityThread.main(ActivityThread.java:3691)
EZSettings(14788): java.lang.reflect.Method.invokeNative(Native Method)
EZSettings(14788): java.lang.reflect.Method.invoke(Method.java:507)
EZSettings(14788): com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:847)
EZSettings(14788): com.android.internal.os.ZygoteInit.main(ZygoteInit.java:605)
EZSettings(14788): dalvik.system.NativeStart.main(Native Method)