Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/365.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/180.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 是不是;“尝试使用资源语句”;真的可以使用android光标对象吗?_Java_Android_Cursor_Android Sqlite_Try With Resources - Fatal编程技术网

Java 是不是;“尝试使用资源语句”;真的可以使用android光标对象吗?

Java 是不是;“尝试使用资源语句”;真的可以使用android光标对象吗?,java,android,cursor,android-sqlite,try-with-resources,Java,Android,Cursor,Android Sqlite,Try With Resources,由于Java从1.7开始引入此语句,我采用此规则编写与游标相关的语句,如下所示: try (Cursor cursor = context.getContentResolver().query(queryAccountUri, null, null, null, null)) { if (cursor != null && cursor.moveToFirst()) { entry.userId = cursor.getSt

由于Java从1.7开始引入此语句,我采用此规则编写与游标相关的语句,如下所示:

try (Cursor cursor = context.getContentResolver().query(queryAccountUri, null, null, null, null)) {
            if (cursor != null && cursor.moveToFirst()) {
                entry.userId = cursor.getString(0);
                entry.account = cursor.getString(1);
                entry.phone = cursor.getString(2);
                entry.nickName = cursor.getString(3);
                entry.icon = cursor.getString(4);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
try {
            if (cursor.moveToFirst()) {
                entry.userId = cursor.getString(0);
                entry.account = cursor.getString(1);
                entry.phone = cursor.getString(2);
                entry.nickName = cursor.getString(3);
                entry.icon = cursor.getString(4);
            }
        } catch (NullPointerException e) {
            e.printStackTrace();
        } finally {
            try {
                cursor.close();
            } catch (NullPointerException e) {
                e.printStackTrace();
            }
        }
然而,这些天来,我们的测试人员不断告诉我,在运行monkey测试时,该语句仍然会导致内存泄漏,甚至在对该方法进行循环测试时会耗尽所有内存。所有日志文件都表明,由于某些
未关闭
问题,
光标
对象就是凶手。因此,我必须更改代码以手动关闭
光标
对象,如下所示:

try (Cursor cursor = context.getContentResolver().query(queryAccountUri, null, null, null, null)) {
            if (cursor != null && cursor.moveToFirst()) {
                entry.userId = cursor.getString(0);
                entry.account = cursor.getString(1);
                entry.phone = cursor.getString(2);
                entry.nickName = cursor.getString(3);
                entry.icon = cursor.getString(4);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
try {
            if (cursor.moveToFirst()) {
                entry.userId = cursor.getString(0);
                entry.account = cursor.getString(1);
                entry.phone = cursor.getString(2);
                entry.nickName = cursor.getString(3);
                entry.icon = cursor.getString(4);
            }
        } catch (NullPointerException e) {
            e.printStackTrace();
        } finally {
            try {
                cursor.close();
            } catch (NullPointerException e) {
                e.printStackTrace();
            }
        }

我已经查过了,现在我想知道这个语句是否真的适用于一般的“可关闭对象”或那些“纯Java读写器相关对象”?

如果可以发布泄漏日志,效果会更好。