SQLite:重新打开一个已经关闭的对象

时间:2018-10-04 18:19:06

标签: android sqlite

所以我无法在仿真器上重新创建错误,因此我必须使用Fabric错误日志,该错误提示给我以下错误:

.parseJsonFeed(Gallery.java:178)
.onResponse(Gallery.java:94)
.onResponse(Gallery.java:88)

我的原始代码:

    public void parseJsonFeed(JSONObject response) {
    try {
        JSONArray gallery = response.getJSONArray("gallery");
        for (int i = 0; i < gallery.length(); i++) {

            JSONObject json = gallery.getJSONObject(i);
            // Loop for ListView here
            String SQLiteDataBaseQuery = "INSERT INTO images_table (id,cover,title,description,imgurl) VALUES('" + i + "', '" + jr_cover + "', '" + jr_title + "', '" + jr_desc + "', '" + pics + "');";
 LINE 178: sqLiteDatabase.execSQL(SQLiteDataBaseQuery);
        }
    } catch (JSONException e) {
        e.printStackTrace();
    } 
}

我在这里关闭数据库的地方:

@Override
protected void onPostExecute(Void result)
{
sqLiteDatabase.close();
}

第94行:

@Override
public void onResponse(JSONObject response) {
VolleyLog.d(TAG, "Response: " + response.toString());
if (response != null) {
LINE 94: parseJsonFeed(response);
}

第88行:

HttpJSonURL, null, new Response.Listener<JSONObject>() {

经过一些研究,我尝试在try和catch语句之后添加以下代码:

finally {
if (sqLiteDatabase != null) {
sqLiteDatabase.close();
}

并放入“ sqLiteDatabase.close();”作为注释//在我的onPostExecute()中。该代码对我有用,但是我不确定问题是否可以通过这种方式解决。有什么建议吗?

完整错误日志:

#0. Crashed: main
       at android.database.sqlite.SQLiteClosable.acquireReference(SQLiteClosable.java:55)
       at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:2086)
       at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:2032)
       at MyAppparseJsonFeed(Gallery.java:178)
       at MyApp$1.onResponse(Gallery.java:94)
       at MyApp$1.onResponse(Gallery.java:88)
       at com.android.volley.toolbox.JsonRequest.deliverResponse(JsonRequest.java:83)
       at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:106)
       at android.os.Handler.handleCallback(Handler.java:789)
       at android.os.Handler.dispatchMessage(Handler.java:98)
       at android.os.Looper.loop(Looper.java:164)
       at android.app.ActivityThread.main(ActivityThread.java:6938)
       at java.lang.reflect.Method.invoke(Method.java)
       at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)

--

Fatal Exception: java.lang.IllegalStateException: attempt to re-open an already-closed object: SQLiteDatabase: /data/user/0/MyApp/databases/gallery_db
       at android.database.sqlite.SQLiteClosable.acquireReference(SQLiteClosable.java:55)
       at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:2086)
       at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:2032)
       at MyAppparseJsonFeed(Gallery.java:178)
       at MyApp$1.onResponse(Gallery.java:94)
       at MyApp$1.onResponse(Gallery.java:88)
       at com.android.volley.toolbox.JsonRequest.deliverResponse(JsonRequest.java:83)
       at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:106)
       at android.os.Handler.handleCallback(Handler.java:789)
       at android.os.Handler.dispatchMessage(Handler.java:98)
       at android.os.Looper.loop(Looper.java:164)
       at android.app.ActivityThread.main(ActivityThread.java:6938)
       at java.lang.reflect.Method.invoke(Method.java)
       at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)

0 个答案:

没有答案