android - 尝试重新打开一个已经关闭的对象

标签 android android-sqlite

插入表时尝试重新打开一个已经关闭的对象异常

   public class UploadImagesToServer {

    static ImgeURLdb imgURLdb;
    static  Context mContext;
    public static void postImage(String ImageLink, String imageName, Context context) {
        mContext = context;
        imgURLdb = new ImgeURLdb(mContext);
        RequestFuture<JSONObject> future = RequestFuture.newFuture();
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("file",new File(ImageLink));
            Log.i("imageName","Sending Image"+ImageLink);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        JsonObjectRequest request = new JsonObjectRequest(Constants.MediaUpload, jsonObject, future, future);
        RequestQueue requestQueue = Volley.newRequestQueue(context);
        requestQueue.add(request);

        try {
            JSONObject response = future.get(10, TimeUnit.SECONDS);// Blocks for at most 10 seconds.
            String imageURL = response.getString("");
            addImagetoDB(imageName,imageURL);
            Log.i("imageName","getting URL"+imageURL+"");
        } catch (InterruptedException e) {
            // Exception handling
        } catch (ExecutionException e) {
            // Exception handling
        } catch (TimeoutException e) {

            //addImagetoDB(imageName,"imageURL");

            e.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
        }



    }

    private static void addImagetoDB(String imageName, String imageURL) {

        imgURLdb.addImageURL(imageName,imageURL);


    }
}

上面的代码是将url添加到db

 public void addImageURL(String imageName, String imageURL){

    DbHelper mDBhelper = new DbHelper(mContext);
    SQLiteDatabase sqlDB = mDBhelper.getWritableDatabase();
    ContentValues values = new ContentValues();
    String query = "Select id from "+DbHelper.IMAGE_URL+" where "+DbHelper.IMAGE_NAME + "=?";
    String[] args = new String[1];
    args[0] = imageName;

    Cursor myCursur = mDBhelper.myquery(query,args);

    if (myCursur != null && myCursur.getCount() > 0) {
        myCursur.moveToPosition(-1);
        while (myCursur.moveToNext()) {
            values.put(DbHelper.IMAGE_URL,imageURL);
            sqlDB.insert(DbHelper.IMAGE_URL, null, values);

        }
        myCursur.close();

    } else
    {
        Log.e("tag", "Cursor is zero");

    }

    mDBhelper.close();
    sqlDB.close();

}

这是将图像 url 添加到数据库的方法。添加时正在尝试重新打开一个已经关闭的对象异常

最佳答案

我在 DbHelper 中看不到您的“myquery()”方法的实现,但您可能在该方法内关闭了游标,然后将其返回。如果您不在那里关闭光标,应该没问题。

关于android - 尝试重新打开一个已经关闭的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38242388/

相关文章:

java - 如何以编程方式检测android studio中的系统背景触摸

java.lang.IllegalStateException : commit already called 错误

java - 选项卡内容 View 中的可滚动内容

android - 无法在索引处绑定(bind)参数,因为索引超出范围

android - 将表条目绑定(bind)到 SQLite 数据库中的 Spinner

java - Android - 强制搜索 View 以编程方式打开

android - 动态设置搜索对话框文本

java - 尝试在空对象引用上调用虚拟方法 - Android

android - SQLite 无法在频繁的 "SELECT"查询中打开数据库文件(代码 14)

java - Android Studio : SQLite Database Query for getting closest location from coordinates