开发者

Android object file becomes corrupt

I am new to android development and have been trying to create some basic apps to get used to the platform. I have a basic app that allows a user to input words with definitions. The main view is a listview that is populated by an ArrayAdapter which is fed from an ArrayList. When the user chooses to add a word they can input a name and definition, and if that name is not already in the list it is added to the list. The name and definition are then put into a Word object (custom object that implements a linked list to reference all words that have been input). This part of the program works fine, I am able to add as many words as I want, the problem is with the saving. I have a reference to the root of my linked list of word objects in an object called wordlist (this object also holds the ArrayList). I have implemented Serializeable with all of my objects and my plan was to save the entire wordlist object each time a word is added. To do this I implemented the following in my Add_Word activity (the activity called when the user chooses to add a word):

try                     
{                       
    FileOutputStream fileOut = openFileOutput("words.obj", Context.MODE_PRIVATE);
    ObjectOutputStream objectOut = new ObjectOutputStream(fileOut);
    objectOut.writeObject(Activity_Main.wordlist);
    objectOut.close();
    fileOut.close();
} 
catch (IOException e) 
{
    Context context = getApplicationContext();
    CharSequence text = "File Error";
    int duration = Toast.LENGTH_SHORT;
    Toast toast = Toast.makeText(context, text, duration);
    toast.show();
}

Now this code does seem to work for the most part, I am able to save and load my words without any problems until for some reason the 22nd word I try to add force closes the program. Then when I try to open it again it will force close as soon as it pops up. This is also weird to me because I have a try catch statement in the onCreate method of the starting activity that should catch any problem with the file and simply recreate the object:

try
{
            FileInputStream fileIn = this.openFileInput("words.obj");
            ObjectInputStream objectIn = new ObjectInputStream(fileIn);
            wordlist = (Words) objectIn.readObject();
            objectIn.close();
            fileIn.close();
}
catch (Exception e)
{
    wordlist = new Words();
}

If I go into my applications on my phone and clear the data it is able to start back up. I could not find anyone who had a similar problem (as far as I could tell). I did try commenting out the file output section and everything works fine, it just wont save. Any ideas would be greatly appreciated. (In case it is not clear I am trying to save locally to my program).

My development environment: Windows 7 64bit, Eclipse 64bit, Java 64bit (I know some people have had problems with 64bit Java and Eclipse, but I have been able to develop other Java and Android programs in this environment). I used the Android 2.1 update 1 emulator, and it does the same thing in the emulator. I also had an Android 2.1 update 1 phone which had the same problem, I have since updated to 2.2 and still the same problem.

UPDATE: Thanks for the responses, I looked at the log and this is what I got when the program crashed:

04-28 18:04:27.629: ERROR/AndroidRuntime(16570): FATAL EXCEPTION: main

04-28 18:04:27.629: ERROR/AndroidRuntime(16570): java.lang.StackOverflowError

04-28 18:04:27.629: ERROR/AndroidRuntime(16570): at java.util.IdentityHashMap.findIndex(IdentityHashMap.java:419)

04-28 18:04:27.629: ERROR/AndroidRuntime(16570): at java.util.IdentityHashMap.get(IdentityHashMap.java:371)

04-28 18:04:27.629: ERROR/AndroidRuntime(16570): at java.io.ObjectOutputStream.dumpCycle(ObjectOutputStream.java:478)

04-28 18:04:27.629: ERROR/AndroidRuntime(16570): at java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1751)

04-28 18:04:27.629: ERROR/AndroidRuntime(16570): at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1701)

04-28 18:04:27.629: ERROR/AndroidRuntime(16570): at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1665)

04-28 18:04:27.629: ERROR/AndroidRuntime(16570): at java.io.ObjectOutputStream.writeFieldValues(ObjectOutputStream.java:1153)

04-28 18:04:27.629: ERROR/AndroidRuntime(16570): at java.io.ObjectOutputStream.defaultWriteObject(ObjectOutputStream.java:420)

04-28 18:04:27.629: ERROR/AndroidRuntime(16570): at java.io.ObjectOutputStream.writeHierarchy(ObjectOutputStream.java:1251)

04-28 18:04:27.629: ERROR/AndroidRuntime(16570): at java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1587)

04-28 18:04:27.629: ERROR/AndroidRuntime(16570): at java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1859) 04-28 18:04:27.629: ERROR/AndroidRuntime(16570): at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1701)

04-28 18:04:27.629: ERROR/AndroidRuntime(16570): at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1665)

04-28 18:04:27.629: ERROR/AndroidRuntime(16570): at java.io.ObjectOutputStream.writeFieldValues(ObjectOutputStream.java:1153)

04-28 18:04:27.629: ERROR/AndroidRuntime(16570): at java.io.ObjectOutputStream.defaultWriteObject(ObjectOutputStream.java:420)

04-28 18:04:27.629: ERROR/AndroidRuntime(16570): at java.io.ObjectOutputStream.writeHierarchy(ObjectOutputStream.java:1251)

04-28 18:04:27.629: ERROR/AndroidRuntime(16570): at java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1587)

04-28 18:04:27.629: ERROR/AndroidRuntime(16570): at java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1859)

04-28 18:04:27.629: ERROR/AndroidRuntime(16570): at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1701)

04-28 18:04:27.629: ERROR/AndroidRuntime(16570): at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1665)

04-28 18:04:27.629: ERROR/AndroidRuntime(16570): at java.io.ObjectOutputStream.writeFieldValues(ObjectOutputStream.java:1153)

04-28 18:04:27.629: ERROR/AndroidRuntime(16570): at java.io.ObjectOutputStream.defaultWriteObject(ObjectOutputStream.java:420)

04-28 18:04:27.629: ERROR/AndroidRuntime(16570): at java.io.ObjectOutputStream.writeHierarchy(ObjectOutputStream.java:1251)

04-28 18:04:27.629: ERROR/AndroidRuntime(16570): at java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1587)

04-28 18:04:27.629: ERROR/AndroidRuntime(16570): at java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1859)

04-28 18:04:27.629: ERROR/AndroidRuntime(16570): at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1701)

04-28 18:04:27.629: ERROR/AndroidRuntime(16570): at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1665)

04-28 18:04:27.629: ERROR/AndroidRuntime(16570): at java.io.ObjectOutputStream.writeFieldValues(ObjectOutputStream.java:1153)

04-28 18:04:27.629: ERROR/AndroidRuntime(16570): at java.io.ObjectOutputStream.defaultWriteObject(ObjectOutputStream.java:420)

04-28 18:04:27.629: ERROR/AndroidRuntime(16570): at java.io.ObjectOutputStream.writeHierarchy(ObjectOutputStream.java:1251)

04-28 18:04:27.629: ERROR/AndroidRuntime(16570): at java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1587)

04-28 18:04:27.629: ERROR/AndroidRuntime(16570): at java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1859)

04-28 18:04:27.629: ERROR/AndroidRuntime(16570): at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1701)

04-28 18:04:27.629: ERROR/AndroidRuntime(16570): at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1665)

04-28 18:04:27.629: ERROR/AndroidRuntime(16570): at java.io.ObjectOutputStream.writeFieldValues(ObjectOutputStream.java:1153)

04-28 18:04:27.629: ERROR/AndroidRuntime(16570): at java.io.ObjectOutputStream.defaultWriteObject(ObjectOutputStream.java:420)

04-28 18:04:27.629: ERROR/AndroidRuntime(16570): at java.io.ObjectOutputStream.writeHierarchy(ObjectOutputStream.java:1251)

04-28 18:04:27.629: ERROR/AndroidRuntime(16570): at java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1587)

04-28 18:04:27.629: ERROR/AndroidRuntime(16570): at java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1859)

04-28 18:04:27.629: ERROR/AndroidRuntime(16570): at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1701)

04-28 18:04:27.629: ERROR/AndroidRuntime(16570): at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1665)

04-28 18:04:27.629: ERROR/AndroidRuntime(16570): at java.io.ObjectOutputStream.writeFieldValues(ObjectOutputStream.java:1153)

04-28 18:04:27.629: ERROR/AndroidRuntime(16570): at java.io.ObjectOutputStream.defaultWriteObject(ObjectOutputStream.java:420)

04-28 18:04:27.62开发者_Go百科9: ERROR/AndroidRuntime(16570): at java.io.ObjectOutputStream.writeHierarchy(ObjectOutputStream.java:1251)

04-28 18:04:27.629: ERROR/AndroidRuntime(16570): at java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1587)

04-28 18:04:27.629: ERROR/AndroidRuntime(16570): at java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1859)

04-28 18:04:27.629: ERROR/AndroidRuntime(16570): at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1701)

04-28 18:04:27.629: ERROR/AndroidRuntime(16570): at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1665)

04-28 18:04:27.629: ERROR/AndroidRuntime(16570): at java.io.ObjectOutputStream.writeFieldValues(ObjectOutputStream.java:1153)

04-28 18:04:27.629: ERROR/AndroidRuntime(16570): at java.io.ObjectOutputStream.defaultWriteObject(ObjectOutputStream.java:420)

04-28 18:04:27.629: ERROR/AndroidRuntime(16570): at java.io.ObjectOutputStream.writeHierarchy(ObjectOutputStream.java:1251)

04-28 18:04:27.629: ERROR/AndroidRuntime(16570): at java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1587)

04-28 18:04:27.629: ERROR/AndroidRuntime(16570): at java.io.ObjectOutputStream.writeObjectInternal(ObjectOut

So by this I am assuming that there is a memory problem, but I am still confused as to why this is happening with such a small amount of data. I will look into using a DB but I would appreciate any more ideas of what is causing this.


You're probably running into memory problems by serializing your object. Have you tried it using a database?

It would also be helpful if you'd include any errors your getting from LogCat.


I think this question might interest you : StackOverflowError when serializing an object in Java

Simplest solution: don't serialize arraylists. Consider using a database or simple data files.


On my case the problem was I might sometimes read and write asynchronously. The file would be overwritten in the middle of a read, causing EOFException or an internal IndexOutOfBounds in "readObject()".

Adding synchronized to all my read and write methods worked for me.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜