2015-07-19 48 views
0

我有一個listview活動,它通過sqlite數據庫填充數據;然而,每當我進入onPause,然後進入onResume我的應用程序崩潰,我收到此錯誤:「java.lang.IllegalStateException:試圖重新查詢一個已經關閉的遊標[email protected]」。有誰知道如何阻止這個?有沒有一種方法需要在onPause中調用?onPause和onResume之間的應用程序崩潰Listview問題

 @Override 
protected void onResume() { 
    super.onResume(); 
    uGraduateListAdapter = new ArrayAdapter<String>(ListOfAlarms.this, android.R.layout.simple_list_item_1, populateList()); 
    listOfAlarms.setAdapter(uGraduateListAdapter); 
    Log.i(TAG, "Resume was called"); 
} 

@Override 
protected void onPause() { 
    super.onPause(); 

    Log.i(TAG, "Pause was called"); 
    sqliteDatabase.close(); 
} 



public List<String> populateList(){ 

     // We have to return a List which contains only String values. Lets create a List first 
     List<String> uGraduateNamesList = new ArrayList<String>(); 

     // First we need to make contact with the database we have created using the DbHelper class 
     AndroidOpenDbHelper openHelperClass = new AndroidOpenDbHelper(this); 

     // Then we need to get a readable database 
     sqliteDatabase = openHelperClass.getReadableDatabase(); 

     // We need a a guy to read the database query. Cursor interface will do it for us 
     //(String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy) 
     cursor = sqliteDatabase.query(AndroidOpenDbHelper.TABLE_NAME_ALARM, null, null, null, null, null, null); 
     // Above given query, read all the columns and fields of the table 

     startManagingCursor(cursor); 

     // Cursor object read all the fields. So we make sure to check it will not miss any by looping through a while loop 
     while (cursor.moveToNext()) { 
      // In one loop, cursor read one undergraduate all details 
      // Assume, we also need to see all the details of each and every undergraduate 
      // What we have to do is in each loop, read all the values, pass them to the POJO class 
      //and create a ArrayList of undergraduates 

      String alarmName = cursor.getString(cursor.getColumnIndex(AndroidOpenDbHelper.COLUMN_NAME_ALARM_NAME)); 
//   String ugUniId = cursor.getString(cursor.getColumnIndex(AndroidOpenDbHelper.COLUMN_NAME_UNDERGRADUATE_UNI_ID)); 
      String alarmTotalTime = cursor.getString(cursor.getColumnIndex(AndroidOpenDbHelper.COLLUMN_ALARM_TOTALTIME)); 

      // Finish reading one raw, now we have to pass them to the POJO 
      TestAlarm ugPojoClass = new TestAlarm(); 
      ugPojoClass.setTitle(alarmName); 

      ugPojoClass.setTotalTime(alarmTotalTime); 

      // Lets pass that POJO to our ArrayList which contains undergraduates as type 
      pojoArrayList.add(ugPojoClass); 

      // But we need a List of String to display in the ListView also. 
      //That is why we create "uGraduateNamesList" 
      uGraduateNamesList.add(alarmName); 
     } 

     // If you don't close the database, you will get an error 
     sqliteDatabase.close(); 

     return uGraduateNamesList; 
    } 
+0

一些相關的代碼會有幫助。 – AndroidEx

+0

好吧我添加了一些代碼 – leonardo

+0

請同時發佈'populateList()'代碼。 – AndroidEx

回答

2

您正在使用不推薦使用的方法(startManagingCursor()),這很危險。

我如何看到會發生什麼情況:關閉數據庫時(實際上是兩次:在populateList()onPause()中),此數據庫的遊標將變爲無效。但是由於您呼叫startManagingCursor(),您的Activity會保留您的遊標,並在重啓時嘗試呼叫requery(),這會引發錯誤。

儘量不要致電startManagingCursor(),只要cursor.close()完成後即可。或者你可以完全遷移到更新的LoaderManager

+0

是的,這是否感謝您的幫助! – leonardo

+0

太棒了,爲我效勞了! – Julio