Android 对象文件损坏
我是 Android 开发新手,一直在尝试创建一些基本的应用程序来适应该平台。我有一个基本的应用程序,允许用户输入带有定义的单词。主视图是一个由 ArrayAdapter 填充的列表视图,该 ArrayAdapter 由 ArrayList 提供。当用户选择添加单词时,他们可以输入名称和定义,如果该名称尚未在列表中,则会将其添加到列表中。然后将名称和定义放入 Word 对象(实现链接列表以引用已输入的所有单词的自定义对象)。程序的这一部分工作正常,我可以添加任意数量的单词,问题在于保存。我在一个名为 wordlist 的对象中引用了单词对象链接列表的根(该对象还包含 ArrayList)。我已经对所有对象实现了 Serializeable,我的计划是每次添加单词时保存整个单词列表对象。为此,我在 Add_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();
}
现在,此代码似乎在大部分情况下都有效,我可以毫无问题地保存和加载我的单词,直到由于某种原因,我尝试添加强制的第 22 个词关闭了程序。然后,当我尝试再次打开它时,它会在弹出时立即强制关闭。这对我来说也很奇怪,因为我在启动活动的 onCreate 方法中有一个 try catch 语句,它应该捕获文件的任何问题并简单地重新创建对象:
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();
}
如果我进入手机上的应用程序并清除数据能够启动备份。我找不到任何有类似问题的人(据我所知)。我确实尝试注释掉文件输出部分,一切正常,只是无法保存。任何想法将不胜感激。 (如果不清楚我正在尝试本地保存到我的程序)。
我的开发环境:Windows 7 64位、Eclipse 64位、Java 64位(我知道有些人在64位Java和Eclipse上遇到了问题,但我已经能够在这个环境下开发其他Java和Android程序)。我使用了Android 2.1 update 1模拟器,它在模拟器中做了同样的事情。我还有一部 Android 2.1 update 1 手机,也有同样的问题,我已经更新到 2.2,但仍然有同样的问题。
更新: 感谢您的回复,我查看了日志,这是程序崩溃时得到的结果:
04-28 18:04:27.629: ERROR/AndroidRuntime(16570): FATAL EXCEPTION: main
04-28 18:04:27.629:错误/AndroidRuntime(16570): java.lang.StackOverflowError
04-28 18:04:27.629:错误/AndroidRuntime(16570):在java.util.IdentityHashMap.findIndex(IdentityHashMap.java:419)
04-28 18:04:27.629:错误/AndroidRuntime(16570):在java.util.IdentityHashMap.get(IdentityHashMap.java:371)
04-28 18:04:27.629: 错误/AndroidRuntime(16570): 在 java.io.ObjectOutputStream.dumpCycle(ObjectOutputStream.java:478)
04-28 18: 04:27.629:错误/AndroidRuntime(16570):在java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1751)
04-28 18:04:27.629:错误/AndroidRuntime(16570):在java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java) :1701)
04-28 18:04:27.629:错误/AndroidRuntime(16570):在java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1665)
04-28 18:04:27.629:错误/AndroidRuntime(16570):在java.io.ObjectOutputStream .writeFieldValues(ObjectOutputStream.java:1153)
04-28 18:04:27.629:错误/AndroidRuntime(16570):在java.io.ObjectOutputStream.defaultWriteObject(ObjectOutputStream.java:420)
04-28 18:04:27.629:错误/AndroidRuntime(16570):在java.io.ObjectOutputStream.writeHierarchy(ObjectOutputStream.java:1251)
04-28 18:04:27.629:错误/AndroidRuntime(16570):在java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1587)
04-28 18: 04:27.629:错误/AndroidRuntime(16570):在java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1859) 04-28 18:04:27.629:错误/AndroidRuntime(16570):在java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1701)
04-28 18:04:27.629:错误/AndroidRuntime(16570):在java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1665)
04-28 18:04:27.629: 错误/AndroidRuntime(16570): 在 java.io.ObjectOutputStream.writeFieldValues(ObjectOutputStream.java:1153)
04-28 18: 04:27.629:错误/AndroidRuntime(16570):在java.io.ObjectOutputStream.defaultWriteObject(ObjectOutputStream.java:420)
04-28 18:04:27.629:错误/AndroidRuntime(16570):在java.io.ObjectOutputStream.writeHierarchy(ObjectOutputStream.java) :1251)
04-28 18:04:27.629:错误/AndroidRuntime(16570):在java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1587)
04-28 18:04:27.629:错误/AndroidRuntime(16570):在java.io.ObjectOutputStream .writeObjectInternal(ObjectOutputStream.java:1859)
04-28 18:04:27.629:错误/AndroidRuntime(16570):在java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1701)
04-28 18:04:27.629:错误/AndroidRuntime(16570):在java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1665)
04-28 18:04:27.629: 错误/AndroidRuntime(16570): 在 java.io.ObjectOutputStream.writeFieldValues(ObjectOutputStream.java:1153)
04-28 18: 04:27.629:错误/AndroidRuntime(16570):在java.io.ObjectOutputStream.defaultWriteObject(ObjectOutputStream.java:420)
04-28 18:04:27.629:错误/AndroidRuntime(16570):在java.io.ObjectOutputStream.writeHierarchy(ObjectOutputStream.java) :1251)
04-28 18:04:27.629:错误/AndroidRuntime(16570):在java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1587)
04-28 18:04:27.629:错误/AndroidRuntime(16570):在java.io.ObjectOutputStream .writeObjectInternal(ObjectOutputStream.java:1859)
04-28 18:04:27.629:错误/AndroidRuntime(16570):在java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1701)
04-28 18:04:27.629:错误/AndroidRuntime(16570):在java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1665)
04-28 18:04:27.629: 错误/AndroidRuntime(16570): 在 java.io.ObjectOutputStream.writeFieldValues(ObjectOutputStream.java:1153)
04-28 18: 04:27.629:错误/AndroidRuntime(16570):在java.io.ObjectOutputStream.defaultWriteObject(ObjectOutputStream.java:420)
04-28 18:04:27.629:错误/AndroidRuntime(16570):在java.io.ObjectOutputStream.writeHierarchy(ObjectOutputStream.java) :1251)
04-28 18:04:27.629:错误/AndroidRuntime(16570):在java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1587)
04-28 18:04:27.629:错误/AndroidRuntime(16570):在java.io.ObjectOutputStream .writeObjectInternal(ObjectOutputStream.java:1859)
04-28 18:04:27.629:错误/AndroidRuntime(16570):在java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1701)
04-28 18:04:27.629:错误/AndroidRuntime(16570):在java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1665)
04-28 18:04:27.629: 错误/AndroidRuntime(16570): 在 java.io.ObjectOutputStream.writeFieldValues(ObjectOutputStream.java:1153)
04-28 18: 04:27.629:错误/AndroidRuntime(16570):在java.io.ObjectOutputStream.defaultWriteObject(ObjectOutputStream.java:420)
04-28 18:04:27.629:错误/AndroidRuntime(16570):在java.io.ObjectOutputStream.writeHierarchy(ObjectOutputStream.java) :1251)
04-28 18:04:27.629:错误/AndroidRuntime(16570):在java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1587)
04-28 18:04:27.629:错误/AndroidRuntime(16570):在java.io.ObjectOutputStream .writeObjectInternal(ObjectOutputStream.java:1859)
04-28 18:04:27.629:错误/AndroidRuntime(16570):在java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1701)
04-28 18:04:27.629:错误/AndroidRuntime(16570):在java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1665)
04-28 18:04:27.629: 错误/AndroidRuntime(16570): 在 java.io.ObjectOutputStream.writeFieldValues(ObjectOutputStream.java:1153)
04-28 18: 04:27.629:错误/AndroidRuntime(16570):在java.io.ObjectOutputStream.defaultWriteObject(ObjectOutputStream.java:420)
04-28 18:04:27.629:错误/AndroidRuntime(16570):在java.io.ObjectOutputStream.writeHierarchy(ObjectOutputStream.java) :1251)
04-28 18:04:27.629:错误/AndroidRuntime(16570):在java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1587)
04-28 18:04:27.629:错误/AndroidRuntime(16570):在java.io.ObjectOutputStream .writeObjectInternal(ObjectOutputStream.java:1859)
04-28 18:04:27.629:错误/AndroidRuntime(16570):在java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1701)
04-28 18:04:27.629:错误/AndroidRuntime(16570):在java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1665)
04-28 18:04:27.629: 错误/AndroidRuntime(16570): 在 java.io.ObjectOutputStream.writeFieldValues(ObjectOutputStream.java:1153)
04-28 18: 04:27.629:错误/AndroidRuntime(16570):在java.io.ObjectOutputStream.defaultWriteObject(ObjectOutputStream.java:420)
04-28 18:04:27.629:错误/AndroidRuntime(16570):在java.io.ObjectOutputStream.writeHierarchy(ObjectOutputStream.java) :1251)
04-28 18:04:27.629:错误/AndroidRuntime(16570):在java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1587)
04-28 18:04:27.629:错误/AndroidRuntime(16570):在java.io.ObjectOutputStream .writeObjectInternal(ObjectOut
因此,我假设有一个内存问题,但我仍然很困惑为什么这么少量的数据会发生这种情况。我将考虑使用数据库,但我希望有更多关于造成此问题的想法。
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.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(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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可能会因序列化对象而遇到内存问题。您尝试过使用数据库吗?
如果您包含从 LogCat 获得的任何错误,也会很有帮助。
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.
我认为这个问题可能会让您感兴趣:StackOverflowError when serializing an object in Java
最简单的解决方案:不要序列化数组列表。考虑使用数据库或简单的数据文件。
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.
就我而言,问题是我有时可能会异步读写。文件将在读取过程中被覆盖,导致 EOFException 或“readObject()”中的内部 IndexOutOfBounds。
将同步添加到我所有的读写方法对我来说很有效。
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.