StaleDataException:Attempted 在游标关闭后访问游标

StaleDataException:Attempted to access a cursor after it has been closed

在我的应用程序中,我使用 SimpleCursorAdapter 来更新列表视图。此 SimpleCursorAdapter 从 Cursor 对象获取数据。在这里,当我在 onCreate 方法中关闭游标时,它抛出 StaleDataException:Attempted 以在游标关闭后访问它 。但是当我在游标上删除了这个关闭方法然后它没有发生并且它工作得很好。但是如果我们删除游标上的关闭方法那么它不会造成内存泄漏吗?

与cursor.close() -

//the columns which we want to show in list view
    private static final String[] columnsToUse = new String[]{
            UserDictionary.Words.WORD,
            UserDictionary.Words.FREQUENCY};

    // array of item ids where above two entries will be saved
    private static final int[] toView = new int[]{
            android.R.id.text1,
            android.R.id.text2};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //TextView displayTextView = (TextView) findViewById(R.id.display_text);

        ListView dictionaryList = (ListView)findViewById(R.id.dictionary_list_view);

        ContentResolver resolver = getContentResolver();
        Cursor cursor = resolver.query(UserDictionary.Words.CONTENT_URI, null, null, null, null);

        cursor.moveToFirst();



        SimpleCursorAdapter dictionaryAdapter = new SimpleCursorAdapter(this, android.R.layout.two_line_list_item,
                cursor, columnsToUse, toView, 0);

        dictionaryList.setAdapter(dictionaryAdapter);
        cursor.close();

throwing - StaleDataException:Attempted 在游标关闭后访问游标

但是如果我评论这个 cursor.close() 那么这个错误就不会出现 -

   //the columns which we want to show in list view
        private static final String[] columnsToUse = new String[]{
                UserDictionary.Words.WORD,
                UserDictionary.Words.FREQUENCY};

        // array of item ids where above two entries will be saved
        private static final int[] toView = new int[]{
                android.R.id.text1,
                android.R.id.text2};

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            //TextView displayTextView = (TextView) findViewById(R.id.display_text);

            ListView dictionaryList = (ListView)findViewById(R.id.dictionary_list_view);

            ContentResolver resolver = getContentResolver();
            Cursor cursor = resolver.query(UserDictionary.Words.CONTENT_URI, null, null, null, null);

            cursor.moveToFirst();



            SimpleCursorAdapter dictionaryAdapter = new SimpleCursorAdapter(this, android.R.layout.two_line_list_item,
                    cursor, columnsToUse, toView, 0);

            dictionaryList.setAdapter(dictionaryAdapter);
           // cursor.close();

这就是 Cursor.close() 所做的 releases all of its resources and making it completely invalid... 如果您在适配器之后立即调用 Cursor.close() 那么 CursorAdapter 使用?

要解决,

close() 您的 CursoronDestroy()listViewCursorAdapter 将不再需要它。

我想补充一点:

绘制 UI 所需的系统资源应在 activity 的 UI 不再处于焦点时立即释放。由于这发生在调用 onStop() 之后,您应该准备好在 onStop 时立即释放资源。

So use onStop() instead of onDestroy() method to close the cursor.