2011-03-28 134 views
1

在我的应用程序中,有一个表单提交的插入操作。大多数情况下插入操作是成功的。有时插入操作没有发生,然后,它给java.lang.RuntimeException。这里是logcat的详细信息:'打开的文件太多'错误

03-28 10:52:09.260: ERROR/IMemory(1501): cannot dup fd=1023, size=1048576, err=0 (Too many open files) 
03-28 10:52:09.260: ERROR/IMemory(1501): cannot map BpMemoryHeap (binder=0x5919b0), size=1048576, fd=-1 (Bad file number) 
03-28 10:52:09.260: ERROR/JavaBinder(1501): *** Uncaught remote exception! (Exceptions are not yet supported across processes.) 
03-28 10:52:09.260: ERROR/JavaBinder(1501): java.lang.RuntimeException: No memory in memObj 
03-28 10:52:09.260: ERROR/JavaBinder(1501):  at android.database.CursorWindow.native_init(Native Method) 
03-28 10:52:09.260: ERROR/JavaBinder(1501):  at android.database.CursorWindow.<init>(CursorWindow.java:518) 
03-28 10:52:09.260: ERROR/JavaBinder(1501):  at android.database.CursorWindow.<init>(CursorWindow.java:27) 
03-28 10:52:09.260: ERROR/JavaBinder(1501):  at android.database.CursorWindow$1.createFromParcel(CursorWindow.java:493) 
03-28 10:52:09.260: ERROR/JavaBinder(1501):  at android.database.CursorWindow$1.createFromParcel(CursorWindow.java:496) 
03-28 10:52:09.260: ERROR/JavaBinder(1501):  at android.content.ContentProviderNative.onTransact(ContentProviderNative.java:96) 
03-28 10:52:09.260: ERROR/JavaBinder(1501):  at android.os.Binder.execTransact(Binder.java:287) 
03-28 10:52:09.260: ERROR/JavaBinder(1501):  at dalvik.system.NativeStart.run(Native Method) 

在我的代码中可能存在什么问题?

+0

显然有些文件没有关闭,但我认为需要更多的细节才能找到问题。 – trojanfoe 2011-03-28 07:17:47

回答

2

很多时候(如trjanfoe建议),这些问题是因为游标仍然是开放的。我发现我可以通过使用try/finally代码块来消除大部分问题。

Cursor cursor = null; 
try { 
    cursor = db.insert(...); 
    more code here 
} 
finally { 
    if (cursor != null) { 
     cursor.close(); 
     cursor = null; 
    } 
} 

这确保了无论发生什么事情,光标总是被关闭。

+0

我只注意到游标变量超出了“finally”子句的范围。所以我纠正了我的答案。我相信你很快就明白了。 – 2012-06-01 18:06:19

相关问题