1

May I know why do I keep getting this error everytime I enter something on the search manager and then press go? This message below always appear on my 5th query when I press the GO button . Why? According to what I had read from this link, Closing the database in a ContentProvider

is that there is not need to close the database if I am using a ContentProvider, but then why do I keep getting this message? Is it because I miss out something ?

01-20 15:05:34.843: E/CursorLeakDetecter(16689): PossibleCursorLeak:content://com.example.android.searchabledict.DictionaryProvider/dictionary,QueryCounter:5

============================================

private void showResults(String query) {

    Cursor cursor = managedQuery(DictionaryProvider.CONTENT_URI, null, null,
                            new String[] {query}, null);

    if (cursor == null) {
        // There are no results
        mTextView.setText(getString(R.string.no_results, new Object[] {query}));
    } else {
        // Display the number of results
        int count = cursor.getCount();
        String countString = getResources().getQuantityString(R.plurals.search_results,
                                count, new Object[] {count, query});
        mTextView.setText(countString);

        // Specify the columns we want to display in the result
        String[] from = new String[] { DictionaryDatabase.KEY_WORD,
                                       DictionaryDatabase.KEY_DEFINITION };

        // Specify the corresponding layout elements where we want the columns to go
        int[] to = new int[] { R.id.word,
                               R.id.definition };

        // Create a simple cursor adapter for the definitions and apply them to the ListView
        SimpleCursorAdapter words = new SimpleCursorAdapter(this,
                                      R.layout.result, cursor, from, to);
        mListView.setAdapter(words);

        // Define the on-click listener for the list items
        mListView.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                // Build the Intent used to open WordActivity with a specific word Uri
                Intent wordIntent = new Intent(getApplicationContext(), WordActivity.class);
                Uri data = Uri.withAppendedPath(DictionaryProvider.CONTENT_URI,
                                                String.valueOf(id));
                wordIntent.setData(data);
                startActivity(wordIntent);
            }
        });
    }
}
Community
  • 1
  • 1
AuroraBlaze
  • 421
  • 2
  • 8
  • 25
  • 1
    you have Cursor leak, not database leak – pskink Jan 20 '14 at 08:39
  • umm, may I know what causes the Cursor to leak and how should I solve it? ty – AuroraBlaze Jan 20 '14 at 08:50
  • lack of cursor.close() ? – pskink Jan 20 '14 at 08:53
  • Well I had tried adding the cursor.close() in the private void showResults(String query) method inside the SearchableDictionary.java file , but when I search for something , it will display empty list. Is it because I close the wrong cursor ? >.< , because according to the searchable dictionary sample, most of the Cursor method are return method. – AuroraBlaze Jan 20 '14 at 08:58
  • i dont know what showResults() you are talking about – pskink Jan 20 '14 at 09:04
  • the show result method consist of these codes, umm , I had added it above. – AuroraBlaze Jan 20 '14 at 09:07
  • In addition, may I also know if it will be dangerous if I don't close the cursor? like memory leaks of RAM overflow or something ? – AuroraBlaze Jan 20 '14 at 09:12
  • You should not get that problem when you use `managedQuery` and (see documentation of that method) you should not close a managed cursor because it will confuse the one that manages it. It's also dangerous to close the cursor since your Adapter is using it after you leave the method. Cursor should stay open while it is in use. I don't see where the error comes from, maybe you have another cursor somewhere? It's not too bad when you see the warning since the system closes the cursor once it detects that. It would be problematic when it does not. Overall better would be to use a CursorLoader. – zapl Jan 20 '14 at 10:13
  • @zapl ty for the hints. I will try the cursorloader ty – AuroraBlaze Jan 20 '14 at 11:48

0 Answers0