0

I am having a little trouble understanding this problem. It only happens when the first time the app is open after a fresh install of the app not an update. On the menu screen I click the see high score button and the app crashes restart the app and it goes to the blank screen as it should no scores are in the db. As far as I can tell it never crashes again, no matter how hard I try. The issue will be when the user first uses the app after downloading it from the store.

Caused by: java.lang.IllegalStateException: Cannot perform this operation because the connection pool has been closed.

public class Top extends ListActivity {
   public static int mScreenWidth, mScreenHeight;

   @Override
   protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);

    //
    WindowManager windowManager = getWindowManager();
    Display display = windowManager.getDefaultDisplay();
    mScreenWidth = display.getWidth();
    mScreenHeight = display.getHeight();


    this.setTitle("HIGH SCORE");
    final SQLiteHelper helpter = new SQLiteHelper(this);
    Cursor cursor = helpter.query();

    String[] from = {"_rank", "_name", "_score"};
    int[] to = {R.id.mid, R.id.mname, R.id.mscore};

    if (cursor != null) {
        SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.show, cursor, from, to);
        ListView listView = getListView();
        listView.setAdapter(adapter);
    }
}

SQLite:

public class SQLiteHelper extends SQLiteOpenHelper{
//
private static final String DB_NAME = "score.db";
private static final String TABLE_NAME = "ScoreTable";
private static final String CREATE_TABLE = " create table "
        + " if not exists " + TABLE_NAME + " (_id integer primary key autoincrement," +
        "_name text,_score Integer,_rank Integer) ";

private SQLiteDatabase mDB;

SQLiteHelper(Context context) {
    super(context, DB_NAME, null, 2);
    // TODO Auto-generated constructor stub
}

@Override
public void onCreate(SQLiteDatabase db) {
    // TODO Auto-generated method stub
    this.mDB = db;
    db.execSQL(CREATE_TABLE);
}


/**
 * 
 * @param values
 */
public void insert(ContentValues values){
    mDB = getWritableDatabase();
    mDB.beginTransaction();//¡
    try{            
        mDB.insert(TABLE_NAME, null, values);   
        mDB.setTransactionSuccessful(); //endTransaction()
    }catch (Exception e) {
        //Log.e(TAG, e.getMessage());
    }finally{
        mDB.endTransaction();//
        mDB.close();
    }
}

/**
 * @param values
 * @param id
 */
public void update(ContentValues values,int id){
    mDB = getWritableDatabase();
    mDB.beginTransaction();//
    try{            
        mDB.update(TABLE_NAME, values, "_id = '"+id+"'", null);
        mDB.setTransactionSuccessful(); //  
    }catch (Exception e) {
        //Log.e(TAG, e.getMessage());
    }finally{
        mDB.endTransaction();//
        close();
    }
}

/**
 * @param id
 */
public void delete(int id){
    try {
        if (mDB == null)
            mDB = getWritableDatabase();
        mDB.delete(TABLE_NAME, "_id=?", new String[] { String.valueOf(id) });
    } catch (Exception e) {
        // TODO: handle exception
    }finally{
        close();
    }

}

/**
 * 
 */
public Cursor query(){
    Cursor c = null;
    try {
        SQLiteDatabase db = getWritableDatabase();
        c = db.query(TABLE_NAME, null, null, null, null, null, "_score desc",null);
    } catch (Exception e) {
        // TODO: handle exception
    }finally{
        close();
    }

    return c;
}

/**
 * @param score
 * @return
 */
public String queryrank(String score){
    String rank = null;
    try {
        SQLiteDatabase db = getWritableDatabase();
        Cursor c = db.query(TABLE_NAME, null, " _score >= '"+score+"'" , null, null, null, null, null);
        rank = String.valueOf(c.getCount());

        Cursor cc = db.query(TABLE_NAME, null, " _score < '"+score+"'" , null, null, null, null, null);
        if(cc.getCount() > 0){
            int mName = cc.getColumnIndex("_name");
            int mScore = cc.getColumnIndex("_score");
            int mRank = cc.getColumnIndex("_rank");
            //String[][] tmpUpdate=new String[cc.getCount()][cc.getColumnCount()];
            String aa ="";
            String bb ="";
            String ee ="";
            for(cc.moveToFirst();!(cc.isAfterLast());cc.moveToNext()){
                if(cc.getString(mName)!=null){
                aa=aa+","+ cc.getString(mName);
                bb =bb+ ","+String.valueOf(cc.getInt(mScore));
                ee =ee+","+String.valueOf(cc.getInt(mRank));
                }
            }
            String[] aaa=aa.split(",");
            String[] bbb=bb.split(",");
            String[] ddd=ee.split(",");
            for(int i=0;i<aaa.length;i++){
                if(aaa[i]!=null && aaa[i]!="" && aaa[i].length()>0){
                Log.v("001",aaa[i]+":"+bbb[i]+":"+ddd[i]);
                ContentValues values=new ContentValues();
                values.put("_name", aaa[i]);
                values.put("_score", Integer.parseInt(bbb[i]));
                values.put("_rank", Integer.parseInt(ddd[i])+1);
                db.update(TABLE_NAME, values, "_score = '"+bbb[i]+"'", null);
                }
            }

        }
    } catch (Exception e) {
        // TODO: handle exception
    }finally{
        close();
    }


    return rank;
}

/**
 * @param nameString
 * @return
 */
public boolean isNameExist(String nameString){
    boolean flag = false;
    mDB = getReadableDatabase();    
    Cursor cursor = mDB.query(TABLE_NAME, null, "_name='"+nameString+"'", null, null, null, null, null);//
    int nameIndex = cursor.getColumnIndex("_name");
    for(cursor.moveToFirst(); !(cursor.isAfterLast()); cursor.moveToNext()){
        if(cursor.getString(nameIndex).equals("") ||cursor.getString(nameIndex) == null){
            flag = false;//
        }else {
            flag = true;//
        }
    }
    cursor.close(); 
    mDB.close();            
    return flag;
}

/**
 * 
 */
public void close() {
    if (mDB != null)
        mDB.close();
}


@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    // TODO Auto-generated method stub
    db.execSQL("drop table if exists " + TABLE_NAME);
    onCreate(db);
}


/**
 * @param model 
 * @param start 
 * @param end 
 * @return 
 */
public Cursor getListViewCursorByModel(int model,String start,String end) {
    Cursor cursor = null;
    try {
        mDB = getWritableDatabase();
        cursor = mDB.query(TABLE_NAME, null, "_model='"+model+"'", null, null, null, "_score desc","'"+start+"','"+end+"'");
    } catch (Exception e) {
        // TODO: handle exception
    }finally{
        mDB.close();
    }       
    return cursor;
  }
}

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.bigtexapps.zookeeper/com.bigtexapps.zookeeper.Top}: java.lang.IllegalStateException: Cannot perform this operation because the connection pool has been closed. at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3253) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3349) at android.app.ActivityThread.access$1100(ActivityThread.java:221) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1794) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:158) at android.app.ActivityThread.main(ActivityThread.java:7224) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120) Caused by: java.lang.IllegalStateException: Cannot perform this operation because the connection pool has been closed. at android.database.sqlite.SQLiteConnectionPool.throwIfClosedLocked(SQLiteConnectionPool.java:1027) at android.database.sqlite.SQLiteConnectionPool.waitForConnection(SQLiteConnectionPool.java:664) at android.database.sqlite.SQLiteConnectionPool.acquireConnection(SQLiteConnectionPool.java:397) at android.database.sqlite.SQLiteSession.acquireConnection(SQLiteSession.java:905) 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:143) at android.database.sqlite.SQLiteCursor.getCount(SQLiteCursor.java:132) at android.widget.CursorAdapter.getCount(CursorAdapter.java:235) at android.widget.ListView.setAdapter(ListView.java:508) at com.bigtexapps.zookeeper.Top.onCreate(Top.java:43) at android.app.Activity.performCreate(Activity.java:6876) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1135) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3206) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3349)  at android.app.ActivityThread.access$1100(ActivityThread.java:221)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1794)  at android.os.Handler.dispatchMessage(Handler.java:102)  at android.os.Looper.loop(Looper.java:158)  at android.app.ActivityThread.main(ActivityThread.java:7224)  at java.lang.reflect.Method.invoke(Native Method)  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120) 

Roger Belk
  • 309
  • 5
  • 17
  • Logcat? Also you are defining the scope of your sqlite helper variable and cursor within the onCreate method. I presume that `helpter` should live as long as the Activity. – Morrison Chang Mar 08 '17 at 17:32
  • No it is done after the game has ended and the submit score button is clicked. This happen before any score is saved to the db. – Roger Belk Mar 08 '17 at 17:44
  • I think you'll have to show what code is in SQLiteHelper class - my guess is: http://stackoverflow.com/questions/7973120/android-can-sqlite-cursors-be-used-after-closing-the-database – Morrison Chang Mar 08 '17 at 18:02
  • I have found the problem.... It's the close(); in the public Cursor query(){ method. Can I get away with not closing it off? or where should I close it ? – Roger Belk Mar 08 '17 at 18:52
  • You are returning a cursor object, you are not supposed to close it until you are done with the cursor object (at least). No way to avoid managing that, but can be encapsulated in another class if that wrapper class is completely responsible for data access (i.e. a local cache of the dataset in use). Also see: http://stackoverflow.com/questions/32561154/sqlite-handler-class-as-singleton and http://stackoverflow.com/questions/6905524/using-singleton-design-pattern-for-sqlitedatabase – Morrison Chang Mar 08 '17 at 19:10

0 Answers0