我正在创建一个应用程序。我收到此错误:
11-08 13:46:24.665:错误/数据库(443):java.lang.IllegalStateException:/data/data/com.testproj/databases/Testdb SQLiteDatabase 创建且从未关闭
我似乎找不到原因,因为它有时会向我显示错误,有时不会。这是我的代码:
public class SQLiteAssistant extends SQLiteOpenHelper {
public SQLiteAssistant(Context context){
super(context, DB_NAME, null, DB_VERSION_NUMBER);
this.myContext = context;
}
public void openDataBase() throws SQLException{
String myPath = DB_PATH + DB_NAME;
myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE);
}
public void closeDataBase() {
if(this.myDataBase != null) {
if(this.myDataBase.isOpen())
this.myDataBase.close();
}
}
}
}
在另一堂课中,我有以下查询:
public class Db{
private static SQLiteAssistant sqlite;
public static String getSomeString(Context ctx) {
sqlite = new SQLiteAssistant(ctx);
sqlite.openDataBase();
Cursor cursor = sqlite.myDataBase.rawQuery("SELECT someColumn from SomeTable",null);
if (cursor != null) {
if (cursor.getCount()==1) {
if(cursor.moveToFirst()) {
String testString = cursor.getString(cursor.getColumnIndex("someColumn"));
cursor.close();
sqlite.closeDataBase();
sqlite.close();
return testString
}
}
}
sqlite.closeDataBase();
sqlite.close();
return null;
}
}
我的问题是当我开始一项新活动时,我得到了一个AsyncTask
. 此任务从 Web 服务获取数据并访问String
. 这是AsyncTask
:
protected class BackTask extends AsyncTask<Context, String, String> {
@Override
protected String doInBackground(Context... params) {
try{
//get requeste data from the database
//access the web service
return result;
} catch (Exception e) {
return null;
}
return null;
}
}
如果我让活动顺其自然,一切都会好起来的。如果我不快速按下后退按钮,我会收到错误消息。关于如何解决这个问题的任何建议?