【问题标题】:Finalizing a Cursor that has not been deactivated or closed完成尚未停用或关闭的光标
【发布时间】:2012-04-12 01:30:34
【问题描述】:

我正在以下代码中从 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)

【问题讨论】:

    标签: android sqlite


    【解决方案1】:

    不要频繁打开和关闭数据库db = adb.openDataBase();,而是关闭数据库关闭光标cursor.close();

    步骤:

    数据库:启动器活动打开数据库,它将保持打开状态,直到活动关闭。 (在 onDestroy() 推荐)

    光标:当光标工作结束时关闭它。

    【讨论】:

    • 谢谢 hotveryspicy 这对我很有帮助。
    【解决方案2】:

    在您的 setOnItemSelectedListener 实现中,您需要关闭光标cr.close();

    【讨论】:

      【解决方案3】:

      在这一行中,它显示了未关闭的游标以及它在 db 中保存的行

      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 循环之后

      if (cr.getCount() > 0) {
      

      关闭光标

          cr.close
      

      【讨论】:

        【解决方案4】:

        有这样的习惯:

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

        也写

        startManagingCursor(mCursor);
        

        每次在活动中定义光标对象之后。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-01-22
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多