2017-06-12 15 views
0

パブリッククラスMainActivityはAppCompatActivity/* {Androidのユーザー辞書からデータにアクセス中にカーソルがエラーを起こしていますか?私は、メモリリークが発生しますcusorを閉じていない場合は

// 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(); 
     } 

    } 

} 

を拡張します。しかし、それはコンパイラによって表示されませんでした! ここで何が起こっているのか正確に知りたいですか? ありがとうございます! */

+0

使用し終わるまで、カーソルをアダプタで閉じることはできません – tyczj

答えて

0

カーソルを開くときには、カーソルを閉じる必要があります。そうしないと、システムのグローバルエリアにメモリリークが発生します。
プログラムがデータ構造体のメモリを割り当てた後、そのメモリを解放せず、そのメモリを再利用しないと、メモリリークが発生します。次にプログラムがデータ構造を必要とするときには、さらに多くのメモリーを割り振ります。時間の経過とともに、使用可能なメモリが縮小しているように見えます。これを「メモリリーク」と呼びます。そのデータ構造体を使用してデータ構造体に割り当てられたメモリの割り当てが解除されるようにすることでメモリリークを克服します。

関連する問題