Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/183.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Android中从用户字典访问数据时,游标出现错误?_Android_Android Contentprovider_Android Cursor - Fatal编程技术网

在Android中从用户字典访问数据时,游标出现错误?

在Android中从用户字典访问数据时,游标出现错误?,android,android-contentprovider,android-cursor,Android,Android Contentprovider,Android Cursor,公共类MainActivity扩展了AppCompatActivity{ // For the SimpleCursorAdapter to match the UserDictionary columns to layout items. private static final String[] COLUMNS_TO_BE_BOUND = new String[]{ UserDictionary.Words.WORD, User

公共类MainActivity扩展了AppCompatActivity{

    // For the SimpleCursorAdapter to match the UserDictionary columns to layout items.
    private static final String[] COLUMNS_TO_BE_BOUND = new String[]{
            UserDictionary.Words.WORD,
            UserDictionary.Words.FREQUENCY
    };

    private static final int[] LAYOUT_ITEMS_TO_FILL = new int[]{
            android.R.id.text1,
            android.R.id.text2
    };

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

        // Get the TextView which will be populated with the Dictionary ContentProvider data.
        ListView dictListView = (ListView) findViewById(R.id.dictionary_list_view);

        //TextView dictTextView = (TextView) findViewById(R.id.dictionary_text_view);
        Cursor cursor = null;
        try {

            // Get the ContentResolver which will send a message to the ContentProvider.
            ContentResolver resolver = getContentResolver();

            // Get a Cursor containing all of the rows in the Words table.
            cursor = resolver.query(UserDictionary.Words.CONTENT_URI, null, null, null, null);

            SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
                    android.R.layout.two_line_list_item,
                    cursor,
                    COLUMNS_TO_BE_BOUND,
                    LAYOUT_ITEMS_TO_FILL,
                    0
            );

            dictListView.setAdapter(adapter);
        } catch (Exception e) {
            e.getMessage();
        } finally {
            cursor.close();
        }

    }

}
/* 如果我不关闭cusor,那么内存泄漏就会发生。但是编译器没有显示它! 我想知道这里到底发生了什么? 提前谢谢!
*/

当我们打开光标时,必须将其关闭,否则系统全局区域将出现内存泄漏。

当程序为数据结构分配内存,但从不释放该内存,也不重用该内存时,就会发生内存泄漏。下次程序需要该数据结构时,它会再次分配更多内存。随着时间的推移,可用内存似乎在缩小,这称为“内存泄漏”通过确保为数据结构分配的内存在使用完该数据结构后被释放,可以克服内存泄漏

在使用完适配器中的游标之前,不会关闭它们