Jignesh Ansodariya
Jignesh Ansodariya

Reputation: 12685

Finalizing a Cursor that has not been deactivated or closed

I am accessing data from sqlite data base in my following code.

@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) {

        }
    });
}

and I am getting error like this.

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)

Upvotes: 0

Views: 4060

Answers (4)

dont open and close your database frequently db = adb.openDataBase(); instead closing database close your cursor cursor.close();

Step:

Database : Launcher Activity opens Database and it will remains open until the Activity gets closed. (in onDestroy() recommended)

Cursor: When the work of cursor gets over close it.

Upvotes: 4

waqaslam
waqaslam

Reputation: 68187

In your setOnItemSelectedListener implementation, you need to close your cursor cr.close();

Upvotes: 0

Hiral Vadodaria
Hiral Vadodaria

Reputation: 19250

Have habit of doing it like:

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

and also write

startManagingCursor(mCursor);

after you define your cursor object in activity everytime.

Upvotes: 1

5hssba
5hssba

Reputation: 8079

In this line it is showing the cursor which was not closed and the row it is holding in 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

In setOnItemSelectedListener method close the cursor.. after this if loop

if (cr.getCount() > 0) {

close the cursor

    cr.close

Upvotes: 0

Related Questions