Degosa
Degosa

Reputation: 13

Finalizing a Cursor that has not been deactivated or closed

Okay, i am new for android developping, or any developping and getting this error. I am trying to do is everytime user select a spinner item then program creates a list. I have an DatabaseHelper class that i am using for getting results of the list. But the problem is it is never create a list, and it gives this error.

DatabaseHelper Methods that activity uses;

 public int[] searchbyLetter(String letter) {
    // TODO Auto-generated method stub
    int[] results = new int[100];
    int count = 0;

    String[] columns = new String[] { KEY_MINERALID };
    Cursor c = minerals.query(DATABASE_TABLE, columns, KEY_LETTER + "= '"
            + letter + "'", null, null, null, null);
    if (c.moveToFirst()) {
        results[count] = c.getInt(c.getColumnIndex(KEY_MINERALID));
    }

    while (c.moveToNext()) {
        count++;
        results[count] = c.getInt(c.getColumnIndex(KEY_MINERALID));
    }
    c.close();
    return results;
}

 public String getName(int _id) {
    // TODO Auto-generated method stub
    String[] columns = new String[] { KEY_NAME };
    Cursor c = minerals.query(DATABASE_TABLE, columns, KEY_MINERALID + "="
            + _id, null, null, null, null);
    c.moveToFirst();
    int iName = c.getColumnIndex(KEY_NAME);
    String result = c.getString(iName);
    c.close();
    return result;
}

This my ListActivity class;

public class Az extends ListActivity {

String[] spAzPaht = { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J",
        "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W",
        "Y", "Z" };
ArrayList<AzOrder> azOrder;
AzOrderAdapter azOrderAdapter;
Spinner spAZ;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.az);
    ArrayAdapter<String> spAzAdpt = new ArrayAdapter<String>(Az.this,
            android.R.layout.simple_spinner_item, spAzPaht);

    spAZ = (Spinner) findViewById(R.id.spAZ);
    spAZ.setAdapter(spAzAdpt);
    azOrder = new ArrayList<AzOrder>();
    this.azOrderAdapter = new AzOrderAdapter(Az.this, R.layout.az, azOrder);
    setListAdapter(azOrderAdapter);

    spAZ.setOnItemSelectedListener(new OnItemSelectedListener() {

        public void onItemSelected(AdapterView<?> arg0, View arg1,
                int arg2, long arg3) {

            getSearches();

        }

        public void onNothingSelected(AdapterView<?> arg0) {
            // TODO Auto-generated method stub

            getSearches();

        }
    });

}


private void getSearches() {
    DatabaseHelper myDBHelper = new DatabaseHelper(Az.this);

    try {
        myDBHelper.createDataBase();
    } catch (IOException e) {
        // TODO: handle exception
        e.printStackTrace();
    }
    try {
        myDBHelper.openDataBase();
    } catch (SQLException sqle) {
        throw sqle;
        // TODO: handle exception
    }
    int position = spAZ.getSelectedItemPosition();
    String letter = spAzPaht[position];
    int[] results = myDBHelper.searchbyLetter(letter);

    try {

        azOrder = new ArrayList<AzOrder>();
        AzOrder AZO = new AzOrder();
        for (int i = 0; i < results.length; i++) {
            AZO.setName(myDBHelper.getName(results[i]));
            AZO.setMineral_ID(results[i]);
            azOrder.add(AZO);
        }
        Thread.sleep(5000);
        Log.i("Array", "" + azOrder.size());
    } catch (Exception e) {
        // TODO: handle exception
        Log.e("BACKGROUND_PROC", e.getMessage());
    }
    myDBHelper.close();


}

This is the error that i am getting

    E/Cursor(295): Finalizing a Cursor that has not been deactivated or 
    closed. database = /data/data/com.mineralidentifier.degosa.test1/databases/
    minerals, table = minerals, query = SELECT minerals_name FROM minerals 
    WHERE _id=0

    E/Cursor(295): android.database.sqlite.DatabaseObjectNotClosedException: 
    Application did not close the cursor or database object that was opened 
    here

    E/Cursor(295):  at android.database.sqlite.SQLiteCursor.<init>
    (SQLiteCursor.java:210)

    E/Cursor(295):  at android.database.sqlite.SQLiteDirectCursorDriver.query
    (SQLiteDirectCursorDriver.java:53)

Upvotes: 1

Views: 1814

Answers (1)

chubbsondubs
chubbsondubs

Reputation: 38706

Looks like the Cursor isn't being closed. I see you have the c.close(), but might not reach it because another exception is being thrown before it. And, that exception is being masked by the DatabaseObjectNotClosedException ends up being thrown masking the original one. It's best to wrap that in a finally clause to ensure it always is closed regardless of an exception being thrown or not.

Cursor c = ...
try {
   ...
} finally {
   c.close();
}

If you do that then it may tell you what the real problem is. Unfortunately, since we don't see the full exception's stacktrace we can't see where exactly DatabaseObjectNotClosedException is thrown from in your code. We can only see where in the android code it's thrown from, but we can't see past SQLiteDirectCursorDriver.query.

Upvotes: 1

Related Questions