“打开文件太多”错误

时间:2011-03-28 07:14:09

标签: android cursor runtimeexception

在我的应用中,表单提交上有一个插入操作。大多数情况下插入操作是成功的。有时插入操作不会发生,然后它会给出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)

我的代码中可能出现什么问题?

1 个答案:

答案 0 :(得分:2)

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

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

这确保无论发生什么情况,光标都将始终关闭。