【问题标题】:App crashes between onPause and onResume Listview issue应用程序在 onPause 和 onResume Listview 问题之间崩溃
【发布时间】:2015-07-19 21:27:51
【问题描述】:

我有一个 listview 活动,它通过 sqlite 数据库填充数据;但是,每当我输入 onPause 然后进入 onResume 时,我的应用程序就会崩溃,并且我会收到此错误:“java.lang.IllegalStateException:尝试重新查询已经关闭的游标 android.database.sqlite.SQLiteCursor@418106a8”。有人知道如何阻止这种情况吗?有没有我必须调用 onPause 的方法?

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

【问题讨论】:

  • 一些相关代码会有所帮助。
  • 好的,我添加了一些代码
  • 请同时发布populateList()代码。
  • 哦,是的,忘记了,好吧,我添加了它

标签: android sqlite listview onresume onpause


【解决方案1】:

您正在使用已弃用的方法 (startManagingCursor()),这很危险。

我怎么看会发生什么:当您关闭数据库时(实际上是两次:在populateList()onPause()),您对该数据库的游标变得无效。但是由于您调用了startManagingCursor(),因此您的Activity 会保留您的光标并在重新启动时尝试对其调用requery(),这会引发错误。

尽量不要打电话给startManagingCursor(),当你完成后只打电话给cursor.close()。或者您可以完全迁移到更新的LoaderManager

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-01-05
    • 1970-01-01
    • 1970-01-01
    • 2015-01-05
    • 2015-04-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多