Firstable I'm sorry for my poor english. I'm developing an application in java for android, using libgdx. I want to write on a file to save some of the applications objects, and I'm using a ObjectOutputStream to do that. Here is an extract of the code:
try {
os_savedGame = new ObjectOutputStream(new FileOutputStream("savedGame.txt"));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
os_savedGame.writeObject(obj1);
os_savedGame.writeObject(obj2);
} catch (IOException e) {
e.printStackTrace();
}
I ran it as a Desktop application and so far so good, but when I tried to run as an Android App I got the following error: Attempt to invoke virtual method 'void java.io.ObjectOutputStream.writeObject(java.lang.Object)' on a null object reference
I know that on the Desktop version, the file is stored in the android\\assets folder, but I looked for the equivalent folder in my mobile phone and I couldn't find it. I searched for data/name_of_my_project in both internal and external memory and I wasn't able to find it. As previously I had a similar error, when I wasn't able to load a skin in the android version (saying that the skin doesn't exist) while I could in the desktop version, I think that my problem might be something related where the files are being stored in the android version. Can someone give me a tip on that please?
Thanks!
I would recommend that you read the official documentation: https://github.com/libgdx/libgdx/wiki/File-handling
Internal files are relative to the application's root or working directory on desktops, relative to the assets directory on Android, and relative to the core/assets/ directory of your GWT project. These files are read-only.
It mentions that files stored in your assets
folder are read-only. For saving your game state I would recommend using the Preferences
API which allows you to store key-value pairs and retrieve them easily. More information here: https://github.com/libgdx/libgdx/wiki/Preferences .
For converting your objects into strings here is another answer: How to serialize an object into a string
If you still want to write to a file you will have to use local storage which is explained further in the wiki (see top-most link).
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.