Android IllegalStateException:尝试重新打开已关闭的对象:SQLiteDatabase

时间:2013-04-04 22:26:26

标签: android android-sqlite

我知道有几个像这样的问题,但是他们似乎都有不同的方法来解决问题而且没有一个解决了我的问题。

我的主要活动工作正常,加载数据库并填充列表视图。然后我调用第二个活动,当我尝试加载列表视图时问题就出现了。

我已尝试使用start/stop managingcursor(cursor),即使它已被弃用,但它并未解决问题。此外,我尝试在我的主要活动中填充游标和数据库,然后再触发下一个,但这也没有帮助。

这两个类都从ListActivity扩展并遵循相同的顺序:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);        

    //Open db in writing mode
    MySQLiteHelper.init(this);
    MySQLiteHelper tpdbh =
        new MySQLiteHelper(this, "DB", null, 1);


    SQLiteDatabase db = tpdbh.getWritableDatabase();

    checkLocationAndDownloadData(); //this fires a Asynctask that calls method parseNearbyBranches shown bellow

   //I load the data to the ListView in the postExecute method of the asynctask by calling:
    /*
    Cursor cursor = MysSQLiteHelper.getBranchesNames();
    adapter = new SimpleCursorAdapter(this,
            R.layout.row, cursor, fields, new int[] { R.id.item_text },0);
    setListAdapter(adapter);
    */

    ListView lv = getListView();
    lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
     @Override
        public void onItemClick(AdapterView<?> listView, View view,
            int position, long id) {

         // Get the cursor, positioned to the corresponding row in the result set
         Cursor cursor = (Cursor) listView.getItemAtPosition(position);

       // Get the state's capital from this row in the database.
       String branch_id = cursor.getString(cursor.getColumnIndexOrThrow("branch_id"));

           cursor.close();

        openNextActivity(Integer.parseInt(branch_id));
        }
      });
}

//在另一个档案中:

private void parseNearbyBranches(JSONObject jo) throws JSONException
{
 if (   jo.has(jsonTitle) && 
            jo.has("company_id") &&
            jo.has("id")
    ) {
        String branch = jo.getString(jsonTitle);


            MySQLiteHelper tpdbh = MySQLiteHelper.instance;
            SQLiteDatabase db = tpdbh.getWritableDatabase();

            db.execSQL("INSERT INTO Branches (branch_id, name, company_id) " +
                    "VALUES ('" +jo.getInt("id")+"', '" + branch +"', '" +jo.getInt("company_id")+"' )");

            db.close(); //no difference is I comment or uncomment this line

    }
}

在MySQLiteHelper.java中:

public static Cursor getBranchesNames() {
        // TODO Auto-generated method stub
        String[] columns = new String[] { "_id", "branch_id", "name", "company_id" };
        Cursor c = getReadDb().query(branchesTable, columns, null, null, null, null,
                null);            
        return c;
    }

我的其他活动基本相同:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_branch_surveys);

    //Read branch data from DB
        int companyID = -1;
        MySQLiteHelper.init(this);          

        String [] columns = new String [] {"company_id"};
        String [] args = {Integer.toString(branchID)};
        Cursor c = MySQLiteHelper.getReadDb().query(MySQLiteHelper.branchesTable, columns, "branch_id=?", args, null, null, null); //THIS QUERY WORKS JUST FINE

        if (c.moveToFirst())
            companyID = Integer.parseInt(c.getString(0));
        c.close();

        if( companyID != -1)
        {
            new DownloadTask().execute(Integer.toString(companyID) );
//where the Async task calls something just like NearByBranches shown above(but with different  objects of course)
//And the postExecute sets the listView:
/*  cursor = MySQLiteHelper.getAll();
    SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
            R.layout.row, cursor, fields, new int[] { R.id.item_text },0);
    setListAdapter(adapter);  
*/
        }

    }
}

在MySQLiteHelper.java中:

public static Cursor getAll() {
    // TODO Auto-generated method stub
    String[] columns = new String[] { "_id","title", "points" };

//********IT IS IN THIS LINE WHERE I GET THE ERROR:********************
    Cursor c = getReadDb().query(theTable, columns, null, null, null, null,
            null);

    return c;
}

  public static SQLiteDatabase getReadDb() {
        if (null == db) {
            db = instance.getReadableDatabase();
        }
        return db;
    }

我希望你能帮助我。谢谢!

1 个答案:

答案 0 :(得分:2)

  

我刚尝试在parseNeabyBranches的类似方法中评论db.close,问题解决了。但是我在parseNearbyBranches()中得到db.close()时没有同样的错误,你能解释一下为什么吗?

parseNearbyBranches()中,您可以创建一个单独的SQLiteDatabase对象:

SQLiteDatabase db = tpdbh.getWritableDatabase();

由于这是与getReadDb()返回的对象不同的对象,因此您可以(并且应该)关闭它。基本规则是每次致电getWritableDatabase()getReadableDatable()时,您必须拥有匹配的close()声明。