2015-07-19 61 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