原因:java.lang.IllegalStateException:无法执行此操作,因为连接池已在Android中关闭

原因:java.lang.IllegalStateException:无法执行此操作,因为连接池已在Android中关闭,java,android,sqlite,Java,Android,Sqlite,我正试图从sqlite数据库获取列表视图中的所有记录,但由于连接池已关闭,因此获取的异常为IllegalStateException:无法执行此操作。一次又一次。如果我删除db.close()即使遇到同样的问题。如何解决这个问题 这是我在db helper类中的代码 public class MyDbHelper extends SQLiteOpenHelper { public static final String DATABASE_NAME = "ClassNKK.db";

我正试图从sqlite数据库获取列表视图中的所有记录,但由于连接池已关闭,因此获取的异常为IllegalStateException:无法执行此操作。一次又一次。如果我删除db.close()即使遇到同样的问题。如何解决这个问题

这是我在db helper类中的代码

public class MyDbHelper extends SQLiteOpenHelper {

    public static final String DATABASE_NAME = "ClassNKK.db";
    public static final int DATABASE_VERSION = 4;
    private Context context;
    public SQLiteDatabase db;
    String str_Authentication_Token;
    String str_LoginUserId;
    String str_UserName;
    String str_UserRole;


    public MyDbHelper(Context context) {

        super(context, DATABASE_NAME, null, DATABASE_VERSION);

        // TODO Auto-generated constructor stub
        this.context = context;

 @Override
    public void onCreate(SQLiteDatabase db) {.....}


@Override
    public void onUpgrade(SQLiteDatabase db, int arg1, int arg2) {
        db.execSQL("DROP TABLE IF EXISTS " + Table_NAME);
        db.execSQL("DROP TABLE IF EXISTS " + Table_COMPANY);
        db.execSQL("DROP TABLE IF EXISTS " + Table_VesselList);
        db.execSQL("DROP TABLE IF EXISTS " + Table_Inspector_List);
        db.execSQL("DROP TABLE IF EXISTS " + Table_AllPost_Table);
        db.execSQL("DROP TABLE IF EXISTS " + Table_ActivityObjectList);
        db.execSQL("DROP TABLE IF EXISTS " + Table_FollowUpList);
        db.execSQL("DROP TABLE IF EXISTS " + Table_SyncTime);
        db.execSQL("DROP TABLE IF EXISTS " + Table_VEsselHashTag);
        db.execSQL("DROP TABLE IF EXISTS " + Table_FollowUpSettings);

        onCreate(db);


    public boolean update_AllPost(String strActivityStatus, String strActivityId,
                                  String strRemark,
                                  String strName,
                                  String strInspectorName,
                                  String strShortname,
                                  String strHashTag,
                                  String strStatus,
                                  String strType,
                                  String strUserId,
                                  String strObjectId,

                                  String strActiondate,
                                  String strStarFlag,
                                  String strDownLoadStatus,
                                  String strUpLoadStatus,
                                  String strStarMarkUpLoadStatus,
                                  String strUpLoad_FollowUpStatus,
                                  String strUpLoad_UnFollowUpStatus )
    {
        SQLiteDatabase db1 = getWritableDatabase();
        ContentValues args = new ContentValues();
        args.put(COL_AllPost_ActivityStatus, strActivityStatus);
        args.put(COL_AllPost_PostActivityId, strActivityId);
        args.put(COL_AllPost_Remark, strRemark);
        args.put(COL_AllPost_Title, strName);
        args.put(COL_AllPost_inspectorname, strInspectorName);
        args.put(COL_AllPost_shortName, strShortname);
        args.put(COL_AllPost_HashTag, strHashTag);
        args.put(COL_AllPost_Status, strStatus);
        args.put(COL_AllPost_Type, strType);

        args.put(COL_AllPost_UserId, strUserId);
        args.put(COL_AllPost_VesselId, strObjectId);

        args.put(COL_AllPost_ActionDate, strActiondate);
        args.put(COL_AllPost_StarFlag, strStarFlag);
        args.put(COL_AllPost_DownLoadStatus, strDownLoadStatus);
        args.put(COL_ALLPOST_UpLoad_Status , strUpLoadStatus);
        args.put(COL_ALLPOST_UpLoad_StarMark_Status ,strStarMarkUpLoadStatus);
        args.put(COL_ALLPOST_UpLoad_FollowUpStatus ,strUpLoad_FollowUpStatus);
        args.put(COL_ALLPOST_UpLoad_UnFollowUpStatus ,strUpLoad_UnFollowUpStatus);

        int i = db1.update(Table_AllPost_Table, args, COL_AllPost_PostActivityId + "=" + strActivityId, null);

        return i > 0;

    }




    public boolean update_FollowUpPost(String Followup,
                                       String FollowupType,
                                       String activityId,
                                       String createdBy,
                                       String date,
                                       String editableFlag,
                                       String follow_id,
                                       int followupId,
                                       String objectId,
                                       String followUp_UpLoadStatus
                                      )
    {
        SQLiteDatabase db1 = getWritableDatabase();
        ContentValues args = new ContentValues();
        args.put(COL_Followup, Followup);
        args.put(COL_FollowupType , FollowupType);
        args.put(COL_Followup_activityId, activityId);
        args.put(COL_Followup_createdBy, createdBy);
        args.put(COL_Followup_date, date);
        args.put(COL_Followup_editableFlag, editableFlag);
        args.put(COL_FID, follow_id);
        args.put(COL_followupId, followupId);
        args.put(COL_Followup_objectId, objectId);
        args.put(COL_FollowUp_UpLoadStatus , followUp_UpLoadStatus);

        int i = db1.update(Table_FollowUpList, args, COL_followupId + "=" + followupId, null);

        return i > 0;

    }

    }

 public boolean isAllPostId_Exist(String activityId) {

        SQLiteDatabase db = this.getReadableDatabase();

        Cursor cursor = db.query(Table_AllPost_Table, new String[]{COL_AllPost_PostActivityId}, COL_AllPost_PostActivityId + "=?",

                new String[]{String.valueOf(activityId)}, null, null, null, null);

        if (cursor != null) {

            if (cursor.getCount() == 0) {
                cursor.close();
                db.close();
                return false;

            } else {
                cursor.close();
                db.close();
                return true;
            }
        }
        return false;
    }

    public boolean isConpany_IdExit(String strCompanyId) {

        SQLiteDatabase db = this.getReadableDatabase();

        Cursor cursor = db.query(Table_COMPANY, new String[]{COL_Company_Id}, COL_Company_Id + "=?",

                new String[]{String.valueOf(strCompanyId)}, null, null, null, null);

        if (cursor != null) {

            if (cursor.getCount() == 0) {
                //   Log.e("Row is  Not  ", " Exist in Table_VesselList Table in MyDbHelper");

                cursor.close();
                db.close();
                return false;

            } else {

                //  Log.e("Row is   already ", " Exist in Table_VesselList Table in MyDbHelper ");
                cursor.close();
                db.close();
                return true;
            }
        }
        return false;
    }

    public boolean isActivityObject_ImageAudioPath(String str_ImageAudioPath) {

        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = db.query(Table_ActivityObjectList, new String[]{COL_imageaudioPath}, COL_imageaudioPath + "=?",

                new String[]{String.valueOf(str_ImageAudioPath)}, null, null, null, null);

        if (cursor != null) {

            if (cursor.getCount() == 0) {

                cursor.close();
                db.close();
                return false;

            } else {


                cursor.close();
                 db.close();
                return true;
            }
        }
        return false;
    }

    public boolean isExistNotation(String str_Notation) {

        SQLiteDatabase db = this.getReadableDatabase();

        Cursor cursor = db.query(Table_VEsselHashTag, new String[]{VesselHashTag}, VesselHashTag + "=?",

                new String[]{String.valueOf(str_Notation)}, null, null, null, null);

        if (cursor != null) {

            if (cursor.getCount() == 0) {
                //Log.e("Row is Not  ", " Exist in ActivityObjectList Table in MyDbHelper");
                cursor.close();
                db.close();
                return false;

            } else {

                //Log.e("Row is already ", " Exist in ActivityObjectList Table in MyDbHelper ");
                cursor.close();
                db.close();
                return true;
            }
        }
        return false;
    }

 public void insertCompany(String strCompanyId, String strCompanyName) {
        SQLiteDatabase db1 = getReadableDatabase();
        ContentValues values = new ContentValues();
        values.put(COL_Company_Id, strCompanyId);
        values.put(COL_CompanyName, strCompanyName);
        db1.insert(Table_COMPANY, null, values);
        db1.close();
    }

    /*********************************************************************************************************/
    @Override
    public synchronized void close() {
        if (db != null) {
            db.close();
            super.close();
        }
    }

    /********************************************************************************************************/

    public boolean updateStarFlag(String str_ActivityId, String str_StarFlag, String str_StarMark_UpLoadStatus) {
        SQLiteDatabase db1 = getWritableDatabase();
        ContentValues args = new ContentValues();
        args.put(COL_AllPost_PostActivityId, str_ActivityId);
        args.put(COL_AllPost_StarFlag, str_StarFlag);
        args.put(COL_ALLPOST_UpLoad_StarMark_Status, str_StarMark_UpLoadStatus);

        int i = db1.update(Table_AllPost_Table, args, COL_AllPost_PostActivityId + "= ? ", new String[]{str_ActivityId});

        return i > 0;
    }

    public void insert_VesselList(String strACtiveStatus, String strVessCompanyId, String strVesselName, String strVesseleId, String strVesselsTypeId, String VesselsTypeName, String strClassCodeNumber, String strIsFollowedVessels) {

        SQLiteDatabase db = getReadableDatabase();
        ContentValues values = new ContentValues();
        values.put(COL_ACtiveStatus, strACtiveStatus);
        values.put(COL_VessCompanyId, strVessCompanyId);
        values.put(COL_VesselName, strVesselName);
        values.put(COL_VesseleId, strVesseleId);
        values.put(COL_VesselsTypeId, strVesselsTypeId);
        values.put(COL_VesselsTypeName, VesselsTypeName);
        values.put(COL_ClassCodeNumber, strClassCodeNumber);
        values.put(COL_IsFollowedVessels, strIsFollowedVessels);

        db.insert(Table_VesselList, null, values);
        db.close();
    }

    public boolean isUserDetails_Exist(String strUserName) {

        SQLiteDatabase db = this.getReadableDatabase();

        Cursor cursor = db.query(Table_NAME, new String[]{COL_Username}, COL_Username + "=?",

                new String[]{String.valueOf(strUserName)}, null, null, null, null);

        if (cursor != null) {

            if (cursor.getCount() == 0) {
                // Log.e("Row is  Not  ", " Exist in Table_VesselList Table in MyDbHelper");

                cursor.close();
                db.close();
                return false;

            } else {

                //  Log.e("Row is   already ", " Exist in Table_VesselList Table in MyDbHelper ");
                cursor.close();
                db.close();
                return true;
            }
        }
        return false;
    }
 public List<All_Post> getMyAll_StarPost(int OFFSET) {
    List<All_Post> allPostDescList = new ArrayList<All_Post>();
    String selectQuery = "select * from  " + Table_AllPost_Table + " where StarFlag='1' AND ActiveStatus = '1' AND Status = 'PENDING' ORDER BY ActionDate DESC LIMIT 50 OFFSET "+OFFSET ; // LIMIT 50 OFFSET "+OFFSET;

    db = this.getWritableDatabase();
    Cursor cursor = db.rawQuery(selectQuery, null);

    if (cursor.moveToFirst()) {
        do {
            All_Post allPostDesc = new All_Post();
            allPostDesc.setID(cursor.getInt(cursor.getColumnIndex("ALL_ACT_ID")));
            allPostDesc.setStrActiveStatus(cursor.getString(cursor.getColumnIndex("ActiveStatus")));
            allPostDesc.setStrActivityId(cursor.getString(cursor.getColumnIndex("ActivityId")));
            allPostDesc.setStrRemark(cursor.getString(cursor.getColumnIndex("Remark")));
            allPostDesc.setStrProfilePic(cursor.getString(cursor.getColumnIndex("ProfilePICURL")));
            allPostDesc.setStringInspectorname(cursor.getString(cursor.getColumnIndex("inspectorName")));
            allPostDesc.setStrNotationNo(cursor.getString(cursor.getColumnIndex("HashTag")));
            allPostDesc.setStrUserId(cursor.getString(cursor.getColumnIndex("UserId")));
            allPostDesc.setStrvessel_Id(cursor.getString(cursor.getColumnIndex("VesselId")));
            String strSqliteDate = cursor.getString(cursor.getColumnIndex("ActionDate"));
            allPostDesc.setStrStarFlag(cursor.getString(cursor.getColumnIndex("StarFlag")));
            allPostDesc.setStrDownLoadStatus(cursor.getString(cursor.getColumnIndex("DownLoad_Status")));

            if (strSqliteDate != null && !strSqliteDate.isEmpty()) {
                String result = "";
                Date date1 = null;
                DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
                try {
                    date1 = dateFormat.parse(strSqliteDate);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(date1);
                SimpleDateFormat printFormat = new SimpleDateFormat("yyyy-MM-dd");
                result = printFormat.format(calendar.getTime());
                //Log.e("result = ","==========>"+result);
                allPostDesc.setActiondate(result);
            }


            allPostDescList.add(allPostDesc);

        } while (cursor.moveToNext());
        db.close();

    }
    return allPostDescList;
}

public List<All_Post> getMyAllPost(String strUserId , int OFFSET) {
    List<All_Post> allPostDescList = new ArrayList<All_Post>();
    String selectQuery = "select * from  " + Table_AllPost_Table + " where  UserId=" + strUserId + " AND ActiveStatus = '1' AND Status = 'PENDING' ORDER BY ActionDate DESC LIMIT 50 OFFSET "+OFFSET;

    db = this.getWritableDatabase();
    Cursor cursor = db.rawQuery(selectQuery, null);

    if (cursor.moveToFirst()) {
        do {
            All_Post allPostDesc = new All_Post();
            allPostDesc.setID(cursor.getInt(cursor.getColumnIndex("ALL_ACT_ID")));
            allPostDesc.setStrActiveStatus(cursor.getString(cursor.getColumnIndex("ActiveStatus")));
            allPostDesc.setStrActivityId(cursor.getString(cursor.getColumnIndex("ActivityId")));
            allPostDesc.setStrRemark(cursor.getString(cursor.getColumnIndex("Remark")));
            allPostDesc.setStrProfilePic(cursor.getString(cursor.getColumnIndex("ProfilePICURL")));
            allPostDesc.setStringInspectorname(cursor.getString(cursor.getColumnIndex("inspectorName")));
            allPostDesc.setStrNotationNo(cursor.getString(cursor.getColumnIndex("HashTag")));
            allPostDesc.setStrUserId(cursor.getString(cursor.getColumnIndex("UserId")));
            allPostDesc.setStrvessel_Id(cursor.getString(cursor.getColumnIndex("VesselId")));
            String strSqliteDate = cursor.getString(cursor.getColumnIndex("ActionDate"));
            allPostDesc.setStrStarFlag(cursor.getString(cursor.getColumnIndex("StarFlag")));
            allPostDesc.setStrDownLoadStatus(cursor.getString(cursor.getColumnIndex("DownLoad_Status")));


            if (strSqliteDate != null && !strSqliteDate.isEmpty()) {
                String result = "";
                Date date1 = null;
                DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
                try {
                    date1 = dateFormat.parse(strSqliteDate);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(date1);
                SimpleDateFormat printFormat = new SimpleDateFormat("yyyy-MM-dd");
                result = printFormat.format(calendar.getTime());
                //Log.e("result = ","==========>"+result);
                allPostDesc.setActiondate(result);
            }

            allPostDescList.add(allPostDesc);

        } while (cursor.moveToNext());
        db.close();

    }
    return allPostDescList;
}

public List<All_Post> getAllDescriptions(int OFFSET)
{
    List<All_Post> allPostDescList = new ArrayList<All_Post>();
    String selectQuery = "select * from  " + Table_AllPost_Table + " where  ActiveStatus = '1' AND Status = 'PENDING' ORDER BY ActionDate DESC LIMIT 50 OFFSET "+OFFSET; //ORDER BY ActionDate DESC
    db = this.getWritableDatabase();
    Cursor cursor = db.rawQuery(selectQuery, null);

    if (cursor.moveToFirst()) {
        do {
            All_Post allPostDesc = new All_Post();
            allPostDesc.setID(cursor.getInt(cursor.getColumnIndex("ALL_ACT_ID")));
            allPostDesc.setStrActiveStatus(cursor.getString(cursor.getColumnIndex("ActiveStatus")));
            allPostDesc.setStrActivityId(cursor.getString(cursor.getColumnIndex("ActivityId")));
            allPostDesc.setStrRemark(cursor.getString(cursor.getColumnIndex("Remark")));
            allPostDesc.setStrProfilePic(cursor.getString(cursor.getColumnIndex("ProfilePICURL")));
            allPostDesc.setStringInspectorname(cursor.getString(cursor.getColumnIndex("inspectorName")));
            allPostDesc.setStrNotationNo(cursor.getString(cursor.getColumnIndex("HashTag")));
            allPostDesc.setStrShortName(cursor.getString(cursor.getColumnIndex("Type")));
            allPostDesc.setStrUserId(cursor.getString(cursor.getColumnIndex("UserId")));
            allPostDesc.setStrvessel_Id(cursor.getString(cursor.getColumnIndex("VesselId")));

            String strSqliteDate = cursor.getString(cursor.getColumnIndex("ActionDate"));

            if (strSqliteDate != null && !strSqliteDate.isEmpty()) {
                String result = "";
                Date date1 = null;
                DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
                try {
                    date1 = dateFormat.parse(strSqliteDate);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(date1);
                SimpleDateFormat printFormat = new SimpleDateFormat("dd-MM-yyyy");
                result = printFormat.format(calendar.getTime());
                //Log.e("result = ","==========>"+result);
                allPostDesc.setActiondate(result);
            }

            allPostDesc.setStrStarFlag(cursor.getString(cursor.getColumnIndex("StarFlag")));
            allPostDesc.setStrDownLoadStatus(cursor.getString(cursor.getColumnIndex("DownLoad_Status")));
            allPostDescList.add(allPostDesc);
        } while (cursor.moveToNext());
    }
    cursor.close();
    db.close();
    return allPostDescList;
}

    }
公共类MyDbHelper扩展了SQLiteOpenHelper{
公共静态最终字符串数据库\u NAME=“ClassNKK.db”;
公共静态最终int数据库_版本=4;
私人语境;
公共数据库数据库;
字符串str_身份验证_令牌;
字符串str_LoginUserId;
字符串str_用户名;
字符串str_UserRole;
公共MyDbHelper(上下文){
super(上下文、数据库名称、null、数据库版本);
//TODO自动生成的构造函数存根
this.context=上下文;
@凌驾
public void onCreate(SQLiteDatabase db){….}
@凌驾
public void onUpgrade(SQLiteDatabase db,int arg1,int arg2){
db.execSQL(“如果存在删除表”+表名称);
db.execSQL(“如果存在删除表”+表_COMPANY);
db.execSQL(“如果存在,则删除表”+TABLE_VesselList);
db.execSQL(“如果存在删除表”+表检查列表);
db.execSQL(“如果存在删除表”+表\u AllPost\u表);
db.execSQL(“如果存在,则删除表”+表_ActivityObjectList);
db.execSQL(“如果存在,则删除表”+表_FollowUpList);
db.execSQL(“如果存在删除表”+表\u SyncTime);
db.execSQL(“如果存在,则删除表”+TABLE_VEsselHashTag);
db.execSQL(“如果存在删除表”+表的后续设置);
onCreate(db);
公共布尔更新_AllPost(字符串strActivityStatus、字符串strActivityId、,
字符串stremark,
字符串strName,
字符串strInspectorName,
字符串strShortname,
字符串strHashTag,
字符串strStatus,
字符串strType,
串串串串串串串串串串串串串串串串串串串串串串串串串串串串串串串串串串串串串串串串串串串串串串串串串串串串串串串串串串串串串串串串串串串串串串串串串串串串串串串串串串串串串串串串串串串串串串串串串串串串串串串串,
字符串strObjectId,
字符串strActiondate,
字符串strStarFlag,
字符串strDownLoadStatus,
弦乐,
字符串strStarMarkUpLoadStatus,
弦式结构,
字符串结构(未折叠状态)
{
SQLiteDatabase db1=getWritableDatabase();
ContentValues args=新的ContentValues();
参数put(COL_AllPost_ActivityStatus,strActivityStatus);
参数put(COL_AllPost_PostActivityId,strActivityId);
参数put(COL\u AllPost\u备注,STREMARK);
args.put(COL_AllPost_Title,strName);
参数put(COL\u AllPost\u inspectorname,strInspectorName);
参数put(COL_AllPost_shortName,strShortname);
参数put(COL_AllPost_HashTag,strHashTag);
参数put(列所有状态,strStatus);
参数put(COL\u AllPost\u类型、strType);
参数put(COL_AllPost_UserId,strUserId);
参数put(COL\u AllPost\u VesselId,strObjectId);
参数put(COL_AllPost_action date,straction date);
args.put(COL_AllPost_StarFlag,strStarFlag);
参数put(COL_AllPost_DownLoadStatus,strDownLoadStatus);
参数put(COL\u ALLPOST\u UpLoad\u Status,strUpLoadStatus);
参数put(COL_ALLPOST_UpLoad_StarMark_Status,strStarMarkUpLoadStatus);
参数put(COL_ALLPOST_UpLoad_FollowUpStatus,strUpLoad_FollowUpStatus);
参数put(COL_ALLPOST_UpLoad_UnFollowUpStatus,strUpLoad_UnFollowUpStatus);
int i=db1.update(Table_AllPost_Table,args,COL_AllPost_PostActivityId+“=”+strActivityId,null);
返回i>0;
}
公共布尔更新\u FollowUpPost(字符串Followup,
字符串跟随类型,
字符串活动ID,
字符串由,
字符串日期,
字符串editableFlag,
字符串跟随\u id,
int followupId,
字符串objectId,
字符串跟踪\u上载状态
)
{
SQLiteDatabase db1=getWritableDatabase();
ContentValues args=新的ContentValues();
参数put(COL_Followup,Followup);
参数put(COL_FollowupType,FollowupType);
参数put(列后续活动ID,活动ID);
参数put(COL_Followup_createdBy,createdBy);
参数put(列后续日期,日期);
args.put(COL\u Followup\u editableFlag,editableFlag);
参数put(列FID,跟随id);
参数put(COL_followupId,followupId);
参数put(COL\u Followup\u objectId,objectId);
参数put(COL\u FollowUp\u UpLoadStatus,FollowUp\u UpLoadStatus);
inti=db1.update(Table_FollowUpList,args,COL_followupId+“=”+followupId,null);
返回i>0;
}
}
公共布尔值isAllPostId_存在(字符串activityId){
SQLiteDatabase db=this.getReadableDatabase();
Cursor Cursor=db.query(Table_AllPost_Table,新字符串[]{COL_AllPost_PostActivityId},COL_AllPost_PostActivityId+“=?”,
新字符串[]{String.valueOf(activityId)},null,null,null,null);
如果(光标!=null){
if(cursor.getCount()==0){
cursor.close();
db.close();
返回false;