这是错误:
02-08 16:35:00.899:ERROR / Database(468):android.database.sqlite.DatabaseObjectNotClosedException:应用程序未关闭此处打开的游标或数据库对象
除此之外,我是。以下是发生此问题的方法:
public static void getUpdates(String username, Context context) {
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost("http://10.0.2.2/tag/appgetfriendinfo.php");
try {
List<NameValuePair> nVPs = new ArrayList<NameValuePair>();
nVPs.add(new BasicNameValuePair("username", username));
httpPost.setEntity(new UrlEncodedFormEntity(nVPs));
HttpResponse response = httpClient.execute(httpPost);
ResponseHandler<String> rHandler = new BasicResponseHandler();
String result = rHandler.handleResponse(response);
JSONArray jArray = new JSONArray(result);
for(int i = 0; i < jArray.length(); i++) {
JSONObject jObj = jArray.getJSONObject(i);
String userCheck = jObj.getString("username");
TagDBAdapter dbHelper = new TagDBAdapter(context);
dbHelper.open();//OPENING THE DATABASE
Contact contact = new Contact();
String first = jObj.getString("firstname");
String last = jObj.getString("lastname");
String name = first + " " + last;
contact.setUsername(jObj.getString("username"));
contact.setFirstName(first);
contact.setLastName(last);
contact.setName(name);
contact.setPhoneNumber(jObj.getString("phonenumber"));
contact.setEmail(jObj.getString("email"));
contact.setHomePhone(jObj.getString("homephone"));
contact.setWorkPhone(jObj.getString("workphone"));
if(dbHelper.checkForExisting(userCheck) == true) {
dbHelper.createContact(contact);
}
else {
dbHelper.updateContactAuto(userCheck, contact);
}
dbHelper.close();//CLOSING THE DATABASE
}
} catch(ClientProtocolException e) {
Log.e("GETUPDATES", "CPE", e);
e.printStackTrace();
} catch(IOException e) {
Log.e("GETUPDATES", "IOE", e);
e.printStackTrace();
} catch(JSONException e) {
Log.e("GETUPDATES", "JSONE", e);
e.printStackTrace();
}
}
正如你在//评论中注意到的那样,我正在打开并关闭数据库,但我仍然收到错误。这是奇怪的,但错误的来源是在SQLite的open()方法中。
错误/数据库(468):at com.tagapp.android.TagDBAdapter.open(TagDBAdapter.java:62)
这是:
/**THESE ARE MY DBADAPTER'S OPEN AND CLOSE METHODS*/
public TagDBAdapter open() throws SQLException {
mDBHelper = new DatabaseHelper(m_context);
mDb = mDBHelper.getWritableDatabase();
return this;
}
public void close() {
mDBHelper.close();
}
这些直接来自Google的记事本教程,他们在不同的情况下为我100%工作。有没有人知道这里发生了什么?非常感谢。
答案 0 :(得分:12)
问题不在于数据库对象,而是光标 - 你在某处有一个打开的光标。
确保在关闭数据库之前关闭所有游标。 (顺便说一句,如果你想要花哨,可以创建一个ContentProvider,使用SQLiteOpenHelper而不用担心关闭它。)
答案 1 :(得分:0)
在调查类似问题时,请仔细研究。我注意到的一种可能性是,如果你因为任何原因在循环中抛出异常,那么即使你正确处理它,你也会退出循环而最后一个open()永远不会有相应的关闭。 要解决此问题,您需要将close()语句放在finally {}块中。此外,我没有看到为循环的每次迭代创建和打开新游标的任何理由。将它移到循环外面,这样只有一个open()和相应的close()是finally块中的那个。 (或者你可以在循环中使用另一个try-catch构造,这样close()总是会运行。)
示例:
public static void getUpdates(String username, Context context) {
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost("http://10.0.2.2/tag/appgetfriendinfo.php");
TagDBAdapter dbHelper; // DECLARE THIS HERE SO IT CAN BE ACCESSED OUTSIDE OF try BLOCK
try {
List<NameValuePair> nVPs = new ArrayList<NameValuePair>();
nVPs.add(new BasicNameValuePair("username", username));
httpPost.setEntity(new UrlEncodedFormEntity(nVPs));
HttpResponse response = httpClient.execute(httpPost);
ResponseHandler<String> rHandler = new BasicResponseHandler();
String result = rHandler.handleResponse(response);
JSONArray jArray = new JSONArray(result);
// MOVED TO OUTSIDE OF LOOP
dbHelper = new TagDBAdapter(context);
dbHelper.open();//OPENING THE DATABASE
for(int i = 0; i < jArray.length(); i++) {
JSONObject jObj = jArray.getJSONObject(i);
String userCheck = jObj.getString("username");
Contact contact = new Contact();
String first = jObj.getString("firstname");
String last = jObj.getString("lastname");
String name = first + " " + last;
contact.setUsername(jObj.getString("username"));
contact.setFirstName(first);
contact.setLastName(last);
contact.setName(name);
contact.setPhoneNumber(jObj.getString("phonenumber"));
contact.setEmail(jObj.getString("email"));
contact.setHomePhone(jObj.getString("homephone"));
contact.setWorkPhone(jObj.getString("workphone"));
if(dbHelper.checkForExisting(userCheck) == true) {
dbHelper.createContact(contact);
}
else {
dbHelper.updateContactAuto(userCheck, contact);
}
}
} catch(ClientProtocolException e) {
Log.e("GETUPDATES", "CPE", e);
e.printStackTrace();
} catch(IOException e) {
Log.e("GETUPDATES", "IOE", e);
e.printStackTrace();
} catch(JSONException e) {
Log.e("GETUPDATES", "JSONE", e);
e.printStackTrace();
} finally {
dbHelper.close();//CLOSING THE DATABASE
}
}