2014-02-19 62 views
2

因此,i'v將嚴格模式添加到項目中,現在試圖修復所有這些問題。但我無法弄清楚這一點。我使用ProviderTestCase2 所以這是一個測試方法,我試圖修復:安卓關閉光標在啓用了StrictMode的測試中

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. 

任何線索都會對您有所幫助! 乾杯。

+0

請出示你的'JobTemplatesProvider.query'方法 – nikis

+0

@nikis查詢代碼添加 – Kamil

+0

能否請您也表明你的'JobTemplatesProviderTestCase.testTaskInsertAndUpdateAndQuerySingleItem'方法?我認爲這個問題的發生是因爲當你摧毀你的活動時你不關閉db.close。 – nikis

回答

0

這裏我找到了一個解決方案。嚴格模式一般如下:

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());