2012-03-28 66 views
0

我正在以下代碼中訪問來自sqlite數據庫的數據。完成尚未停用或關閉的遊標

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.profile); 
    type_spn = (Spinner) findViewById(R.id.type_spn); 
    animal_spn = (Spinner) findViewById(R.id.animal_spn); 
    habitat_txt=(TextView)findViewById(R.id.life_txt); 
    diet_txt=(TextView)findViewById(R.id.habit_txt); 
    discription_txt=(TextView)findViewById(R.id.description_txt); 

    adb = DBAdpter.getAdapterInstance(SecondActivity.this); 
    adb.createdatabase(); 
    db = adb.openDataBase(); 

    cr = db.rawQuery("select distinct type from zoo", new String[] {}); 

    if (cr.getCount() > 0) { 
     cr.moveToFirst(); 
     for (int i = 0; i < cr.getCount(); i++) { 
      String type = cr.getString(0); 
      cr.moveToNext(); 
      type_list.add(type); 
     } 
     db.close(); 
     ArrayAdapter<String> type_add = new ArrayAdapter<String>(
       SecondActivity.this, R.layout.spinnerlayout, type_list); 
     type_spn.setAdapter(type_add); 

    } 

    type_spn.setOnItemSelectedListener(new OnItemSelectedListener() { 

     public void onItemSelected(AdapterView<?> arg0, View arg1, 
       int arg2, long arg3) { 
      animal_list.clear(); 
      String type_name = arg0.getItemAtPosition(arg2).toString(); 
      db = adb.openDataBase(); 
      cr = db.rawQuery("select animal from zoo where type like '" 
        + type_name + "%'", new String[] {}); 

      if (cr.getCount() > 0) { 
       cr.moveToFirst(); 
       for (int i = 0; i < cr.getCount(); i++) { 
        String type = cr.getString(0); 
        cr.moveToNext(); 
        animal_list.add(type); 
       } 
      } 
      db.close(); 
      ArrayAdapter<String> type_add = new ArrayAdapter<String>(
        SecondActivity.this, R.layout.spinnerlayout, 
        animal_list); 
      animal_spn.setAdapter(type_add); 

     } 

     public void onNothingSelected(AdapterView<?> arg0) { 

     } 
    }); 

    animal_spn.setOnItemSelectedListener(new OnItemSelectedListener() { 
     String habitat; 
     String diet; 
     String description; 

     public void onItemSelected(AdapterView<?> arg0, View arg1, 
       int arg2, long arg3) { 
      String animal_name = arg0.getItemAtPosition(arg2).toString(); 

      db = adb.openDataBase(); 
      cr = db.rawQuery(
        "select habitat,diet,discription from zoo where type like '" 
          + animal_name + "%'", new String[] {}); 

      if (cr.getCount() > 0) { 
       Log.v("anim","Test"+cr.getCount()); 
       cr.moveToFirst(); 
       habitat =cr.getString(0); 
       diet =cr.getString(1); 
       description =cr.getString(2); 
      } 
      db.close(); 
      Log.v("anim","Test"+" : "+habitat+" : "+diet+" : " + description); 
      habitat_txt.setText(habitat); 
      diet_txt.setText(diet); 
      discription_txt.setText(description); 

     } 

     public void onNothingSelected(AdapterView<?> arg0) { 

     } 
    }); 
} 

我得到這樣的錯誤。

03-28 18:08:09.356: ERROR/Cursor(4754): Finalizing a Cursor that has not been deactivated or closed. database = /data/data/com.zoobuzz/databases/zoo_buzz.sqlite, table = null, query = select animal from zoo where type like 'Bird 
03-28 18:08:09.356: ERROR/Cursor(4754): %' 
03-28 18:08:09.356: ERROR/Cursor(4754): android.database.sqlite.DatabaseObjectNotClosedException: Application did not close the cursor or database object that was opened here 
03-28 18:08:09.356: ERROR/Cursor(4754):  at android.database.sqlite.SQLiteCursor.<init>(SQLiteCursor.java:210) 
03-28 18:08:09.356: ERROR/Cursor(4754):  at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:53) 
03-28 18:08:09.356: ERROR/Cursor(4754):  at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1345) 
03-28 18:08:09.356: ERROR/Cursor(4754):  at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1315) 
03-28 18:08:09.356: ERROR/Cursor(4754):  at com.zoobuzz.SecondActivity$1.onItemSelected(SecondActivity.java:65) 
03-28 18:08:09.356: ERROR/Cursor(4754):  at android.widget.AdapterView.fireOnSelected(AdapterView.java:864) 
03-28 18:08:09.356: ERROR/Cursor(4754):  at android.widget.AdapterView.access$200(AdapterView.java:42) 
03-28 18:08:09.356: ERROR/Cursor(4754):  at android.widget.AdapterView$SelectionNotifier.run(AdapterView.java:830) 
03-28 18:08:09.356: ERROR/Cursor(4754):  at android.os.Handler.handleCallback(Handler.java:587) 
03-28 18:08:09.356: ERROR/Cursor(4754):  at android.os.Handler.dispatchMessage(Handler.java:92) 
03-28 18:08:09.356: ERROR/Cursor(4754):  at android.os.Looper.loop(Looper.java:123) 
03-28 18:08:09.356: ERROR/Cursor(4754):  at android.app.ActivityThread.main(ActivityThread.java:4627) 
03-28 18:08:09.356: ERROR/Cursor(4754):  at java.lang.reflect.Method.invokeNative(Native Method) 
03-28 18:08:09.356: ERROR/Cursor(4754):  at java.lang.reflect.Method.invoke(Method.java:521) 
03-28 18:08:09.356: ERROR/Cursor(4754):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 
03-28 18:08:09.356: ERROR/Cursor(4754):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 
03-28 18:08:09.356: ERROR/Cursor(4754):  at dalvik.system.NativeStart.main(Native Method) 

回答

4

不打開和關閉數據庫經常db = adb.openDataBase();,而不是關閉數據庫關閉遊標cursor.close();

步驟:

數據庫:啓動活動打開數據庫,並直到活動被關閉,它會保持打開狀態。 (在onDestroy()推薦)

光標:當光標的工作結束它關閉它。

+0

謝謝hotveryspicy這對我來說非常有幫助。 – 2012-03-28 13:14:57

+0

+1它可以幫助我@hotveryspicy – Sathish 2013-03-23 07:12:32

0

在你setOnItemSelectedListener實現,您需要關閉遊標cr.close();

+0

我已經完成了這個請參閱代碼。 – 2012-03-28 13:02:02

+0

哎呀,我的意思是光標,而不是數據庫。檢查我更新的答案 – waqaslam 2012-03-28 13:04:38

0

在這一行則顯示其未關閉光標,它持有分貝行

Finalizing a Cursor that has not been deactivated or closed. database = /data/data/com.zoobuzz/databases/zoo_buzz.sqlite, table = null, query = select animal from zoo where type like 'Bird 

在setOnItemSelectedListener方法關閉遊標.. 此後如果循環

if (cr.getCount() > 0) { 

關閉遊標

cr.close 
1

做它喜歡有個習慣:

...onResume() 
{ 
    db.open(); 
} 
...onPause() 
{ 
    db.close(); 
} 
...onDestroy() 
{ 
    db.close; 
    if(mCursor!=null)mCursor.close(); 
} 

,也寫

startManagingCursor(mCursor); 

您在活動定義光標對象後,每次。

相關問題