android - How do I save/load an object? I've done a lot of searching and can't figure out what's wrong -


i've been searching answer long time , know has been answered, none of "correct" answers have worked me.

in mainactivity do:

savedata save = savedata.load(getapplicationcontext()); 

and pull values out of it.

in savedata have:

public void save(context context) {     try {         fileoutputstream fos = context.openfileoutput("savedata", context.mode_private);         objectoutputstream os = new objectoutputstream(fos);         os.writeobject(this);         os.close();         fos.close();     }     catch (ioexception e)     {         e.printstacktrace();     } }  public static savedata load(context context) {     try {         fileinputstream fis = context.openfileinput("savedata");         objectinputstream = new objectinputstream(fis);         savedata save = (savedata) is.readobject();         is.close();         fis.close();         return save;     }     catch (exception e)     {         e.printstacktrace();         return null;     } } 

in androidmanifest.xml have:

<uses-permission android:name="android.permission.write_external_storage"></uses-permission> 

every time launch app crashes (null pointer - file isn't being loaded properly). have no clue what's wrong code , i've tried @ least half dozen different "correct" answers. know might doing wrong?

edit: think i've serialized properly. savedata implements serializable , has these variable types: class1, int, int, boolean, boolean, boolean, int. class1 implements serializable , has these variable types: arraylist[class2], arraylist[class2]. class2 implements serializable , has these variable types: string, double, double, double, string, int, int, int, int, int, int, int, int, int, int, int, int, int, int, arraylist[class3]. class3 implement serializable , has these variables: string, double, double, double, string, string, uri, file.

i don't know serialization, make sense messed part. maybe uri , file aren't serializable?

i don't know saving/loading in android application aside i've read online on last few days. in head simple "i have object, going throw file in phone, when start app again later i'll grab file , pull object out, simple." it's not simple, i'm not sure part i've got wrong.

1-did class implemented serializable ?
2-you don't need external storage permission since writing objects internal.


Comments

Popular posts from this blog

c# - Better 64-bit byte array hash -

webrtc - Which ICE candidate am I using and why? -

php - Zend Framework / Skeleton-Application / Composer install issue -