2

所以我在项目中添加了严格模式,现在尝试修复所有这些问题。但我想不通这个。我正在使用 ProviderTestCase2 所以这是我试图修复的测试方法:

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 方法:

@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;
    }

但是我仍然遇到关于显式调用 close() 方法的相同问题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.

任何线索都会有所帮助!干杯。

4

1 回答 1

0

在这里我找到了一个解决方案。StrictMode 总是喜欢:

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

只需从 VmPolicy 中删除 detectLeakedSqlLiteObjects()。所以应该是

  StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder().detectDiskReads().detectDiskWrites().detectNetwork().penaltyLog().build());
  StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectLeakedClosableObjects().penaltyLog().penaltyDeath().build());
于 2014-03-27T11:12:55.800 回答