我得到一个数据库对象未闭合异常SQLite中(安卓),但我明确地闭上了数据库...帮助?数据库、但我、闭上、异常

2023-09-13 01:05:45 作者:寂寞、与我为邻

下面是错误:

02-08 16:35:00.899:ERROR /数据库(468):android.database.sqlite.DatabaseObjectNotClosedException:应用程序没有关闭在这里打开的游标或数据库对象的

除,很好,我是。这里是这个问题是存在的方法:

Except, well, I am. Here is the method where this problem is occuring:

    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()方法。

As you can see on the lines I noted in //comments, I am opening AND closing the database, yet I am still getting the error. Here is what is strange though, the error's source is in the SQLite's open() method.

错误/数据库(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();
}

这些都是直接从谷歌的记事本教程中,他们已经为我在不同的情况下,工作100%。有没有人有什么是怎么回事的想法?非常感谢。

These are straight from Google's notepad tutorial, and they have worked 100% for me in different instances. Does anyone have an idea of what is going on here? Thanks a lot.

推荐答案

现在的问题是不是数据库对象,它的指针 - 您有一个打开的游标躺在附近的某个地方

The problem is not the database object, it's the cursor - you have an open cursor lying around somewhere.

请确保您关闭数据库之前,所有游标关闭。 (顺便说一句,如果你想成为幻想,您可以创建一个ContentProvider的,使用SQLiteOpenHelper,而不用担心关闭它。)

Make sure that all cursors are closed before you close the database. (Btw, if you want to be fancy, you can create a ContentProvider, use an SQLiteOpenHelper and not worry about closing it at all.)

 
精彩推荐
图片推荐