0

I am having some issues with a piece of android code, the crash seems to be occurring here c.moveToFirst(). I have tried to get a count, and if the cursor result is greater than 0; then execute that code, but that doesn't help.

I am unable to reproduce this error on my apps (I have tried several scenarios), However, through Google Dev Console, I am seeing that other people that are using my app are getting this error.

Thanks in advance

Crash error:

java.lang.IllegalStateException: 
  at android.database.sqlite.SQLiteConnectionPool.throwIfClosedLocked (SQLiteConnectionPool.java:1198)
  at android.database.sqlite.SQLiteConnectionPool.waitForConnection (SQLiteConnectionPool.java:808)
  at android.database.sqlite.SQLiteConnectionPool.acquireConnection (SQLiteConnectionPool.java:534)
  at android.database.sqlite.SQLiteSession.acquireConnection (SQLiteSession.java:904)
  at android.database.sqlite.SQLiteSession.executeForCursorWindow (SQLiteSession.java:834)
  at android.database.sqlite.SQLiteQuery.fillWindow (SQLiteQuery.java:62)
  at android.database.sqlite.SQLiteCursor.fillWindow (SQLiteCursor.java:152)
  at android.database.sqlite.SQLiteCursor.getCount (SQLiteCursor.java:141)
  at com.andre3.wzlimitdistraction.dao.Dao.read (Dao.java:101)
  at com.andre3.wzlimitdistraction.utils.MonitorService$2.run (MonitorService.java:201)
  at java.util.TimerThread.mainLoop (Timer.java:555)
  at java.util.TimerThread.run (Timer.java:505)

Code thats causing it:

    ArrayList<UserApps> arr = new ArrayList<UserApps>();

    int profileId = 0;
    SQLiteDatabase getDb = db.getReadableDatabase();

    if(getDb.isOpen()) {

        String[] columns = {Dbinfo.app.toString(), Dbinfo.app_name.toString(), Dbinfo.start_duration.toString(), Dbinfo.active_duration.toString(), Dbinfo.start_time.toString(), Dbinfo.profile_id.toString()};

        String selection = Dbinfo.app.toString() + " =? and " + Dbinfo.profile_id.toString() + " <=?";
        String[] selectionArgs = {id, String.valueOf(profileId)};
        Cursor c = getDb.query("user_apps", columns, selection, selectionArgs, null, null, null);

        if((c.getCount() > 0) && (c !=null)) {

            if (c.moveToFirst()) {

                do {
                    UserApps form = new UserApps();

                    form.setPkg(c.getString(c.getColumnIndex(Dbinfo.app.toString())));
                    form.setName(c.getString(c.getColumnIndex(Dbinfo.app_name.toString())));

                    arr.add(form);
                } while (c.moveToNext());
            }
        }

        getDb.close();
        c.close();

    }
    return arr;
yardie
  • 1,583
  • 1
  • 14
  • 29
  • The stacktrace has `getCount()` but the code you posted doesn't. This isn't the code that causes that crash. – laalto Aug 02 '18 at 19:16
  • @laalto - I did state that "I have tried to get a count, and if the cursor result is greater than 0, then execute the code, but that doesn't help." just to clarify, before using `getCount()` in an if block, the crash was occurring at `c.moveToFirst()` - I updated the code to reflect what I have. Thanks – yardie Aug 02 '18 at 22:54
  • Ok - what's `getDb` and how are you initializing / disposing it? – laalto Aug 03 '18 at 04:55
  • @laalto - I have updated my posts with the complete block of code. Thanks for the replies. – yardie Aug 03 '18 at 11:20
  • Not necessarily the issue but you should close cursor first and db only after that. – laalto Aug 03 '18 at 11:27

1 Answers1

0

This is possibly because getColumnIndex can not find the column you specified . You can Refer this Question

End User
  • 792
  • 6
  • 14
  • that's not the case. All the column names are valid. As I stated, the code works fine on my devices, just a few other people that are using my app experiences these crashes. – yardie Aug 02 '18 at 22:57