Question

So i'v added strict mode to project and now trying to fix all those things. But i can't figure out this one. I'm using ProviderTestCase2 So this is a test method which i trying to fix:

public void testTaskInsertAndUpdateAndQuerySingleItem() {
    final ContentValues contentValues = defaultTaskContentValues();

    final Uri uri = mMockContentResolver.insert(TaskTemplatesContract.TaskTemplateColumns.CONTENT_URI, contentValues);
    assertNotNull(uri);

    Cursor cursor = mMockContentResolver.query(TaskTemplatesContract.TaskTemplateColumns.CONTENT_URI, null, "ID=?", new String[]{TASK_ID}, null);
    try {
        assertEquals(1, cursor.getCount());
        contentValues.put(TaskTemplatesContract.TaskTemplateColumns.NAME, "View contact data.");
        int rowsUpdated = mMockContentResolver.update(TaskTemplatesContract.TaskTemplateColumns.CONTENT_URI, contentValues, "NAME=?", new String[]{TASK_NAME});
        assertEquals(1, rowsUpdated);
    } finally {
        cursor.close();
    }

    Cursor c = mMockContentResolver.query(uri, null, null, null, null);
    try {
        assertTrue(c.moveToNext());
        assertEqualsRowColumnStringValue(c, TaskTemplatesContract.TaskTemplateColumns.NAME, "View contact data.");
    } finally {
        c.close();
    }
}

JobTemplatesProvider method:

@Override
public Cursor query(Uri uri, String[] projection, String where, String[] whereArgs, String sortOrder) {
    SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
    String tableName;
    switch (sUriMatcher.match(uri)) {
        case JOB_TEMPLATE_ID:
            qb.setProjectionMap(sJobTemplatesProjectionMap);
            qb.appendWhere(JobTemplatesContract.JobTemplateColumns._ID + "=" + uri.getPathSegments().get(1));
            tableName = JOB_TEMPLATES_TABLE_NAME;
            break;
        case TASK_TEMPLATES:
            qb.setProjectionMap(sTaskTemplatesProjectionMap);
            tableName = TASK_TEMPLATES_TABLE_NAME;
            break;
        default:
            throw new IllegalArgumentException(UNKNOWN_URI + uri);
    }
    qb.setTables(tableName);
    SQLiteDatabase db = mDatabaseHelper.getReadableDatabase();
    Cursor c = qb.query(db, projection, where, whereArgs, null, null, sortOrder);

    c.setNotificationUri(getContext().getContentResolver(), uri);
    return c;
    }

But i'm still getting the same problem about explicitly call of close() method on Cursor:

E/StrictMode( 1246): Releasing statement in a finalizer. Please ensure that you explicitly call close() on your cursor: SELECT position, id, instructions, _id, oid, name, form_id, type, job_id FROM task_templates
E/StrictMode( 1246): android.database.sqlite.DatabaseObjectNotClosedException: Application did not close the cursor or database object that was opened here
E/StrictMode( 1246):    at android.database.sqlite.SQLiteCompiledSql.<init>(SQLiteCompiledSql.java:63)
E/StrictMode( 1246):    at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:83)
E/StrictMode( 1246):    at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:49)
E/StrictMode( 1246):    at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:42)
E/StrictMode( 1246):    at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1356)
E/StrictMode( 1246):    at android.database.sqlite.SQLiteQueryBuilder.query(SQLiteQueryBuilder.java:330)
E/StrictMode( 1246):    at android.database.sqlite.SQLiteQueryBuilder.query(SQLiteQueryBuilder.java:280)
E/StrictMode( 1246):    at org.myapp.databases.providers.JobTemplatesProvider.query(JobTemplatesProvider.java:244)
E/StrictMode( 1246):    at android.content.ContentProvider$Transport.query(ContentProvider.java:187)
E/StrictMode( 1246):    at android.content.ContentResolver.query(ContentResolver.java:262)
E/StrictMode( 1246):    at org.myapp.test.unit.JobTemplatesProviderTestCase.testTaskInsertAndUpdateAndQuerySingleItem(JobTemplatesProviderTestCase.java:166)
E/StrictMode( 1246):    at java.lang.reflect.Method.invokeNative(Native Method)
E/StrictMode( 1246):    at java.lang.reflect.Method.invoke(Method.java:507)
E/StrictMode( 1246):    at junit.framework.TestCase.runTest(TestCase.java:154)
E/StrictMode( 1246):    at junit.framework.TestCase.runBare(TestCase.java:127)
E/StrictMode( 1246):    at junit.framework.TestResult$1.protect(TestResult.java:106)
E/StrictMode( 1246):    at junit.framework.TestResult.runProtected(TestResult.java:124)
E/StrictMode( 1246):    at junit.framework.TestResult.run(TestResult.java:109)
E/StrictMode( 1246):    at junit.framework.TestCase.run(TestCase.java:118)
E/StrictMode( 1246):    at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:169)
E/StrictMode( 1246):    at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:154)
E/StrictMode( 1246):    at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:529)
E/StrictMode( 1246):    at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1448)
W/System.err( 1246): StrictMode VmPolicy violation with POLICY_DEATH; shutting down.

Any clue will be helpful ! Cheers.

Was it helpful?

Solution

Here I find a solution just now. The StrictMode alway like:

    StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder().detectDiskReads().detectDiskWrites().detectNetwork().penaltyLog().build());
    StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectLeakedSqlLiteObjects().detectLeakedClosableObjects().penaltyLog().penaltyDeath().build());

Just remove detectLeakedSqlLiteObjects() from VmPolicy. So it should be

  StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder().detectDiskReads().detectDiskWrites().detectNetwork().penaltyLog().build());
  StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectLeakedClosableObjects().penaltyLog().penaltyDeath().build());
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top