android - 我在 SQLite (Android) 中收到数据库对象未关闭异常,但我明确关闭了我的数据库...帮助?

标签 android sqlite

这里是错误:

02-08 16:35:00.899: 错误/数据库 (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();
    }
}

正如您在我在//comments 中注意到的行中看到的那样,我正在打开和关闭数据库,但我仍然收到错误。奇怪的是,错误的来源是 SQLite 的 open() 方法。

错误/数据库(468):在 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% 有效。有谁知道这里发生了什么?非常感谢。

最佳答案

问题不在于数据库对象,而在于游标 - 您在某处有一个打开的游标。

确保在关闭数据库之前关闭所有游标。 (顺便说一句,如果你想花哨的话,你可以创建一个 ContentProvider,使用 SQLiteOpenHelper 而根本不用担心关闭它。)

关于android - 我在 SQLite (Android) 中收到数据库对象未关闭异常,但我明确关闭了我的数据库...帮助?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4940308/

相关文章:

Android 库 : Release . aar 在使用混淆器时将 classes.jar 清空

android - 如何在 Eclipse 中过滤消息到 Logcat?

android - 如何更改我对 map Activity 的第一 View

安卓。如何在真正的大图像(位图)上写文字并保存

android - Android SQLiteDatabase 线程安全吗?

c - 当另一个程序访问 SQLite 数据库时如何解锁它?

android - Cordova android 插件调试

sql - 多对多关系排序

android - 如何在不在表中执行两次且不使用 'WITH' 子句的情况下获取 SELECT 上每一行的 SUM 值?

objective-c - 从 sqlite 数据库中获取数据时编码损坏