2013-04-04 89 views
2

我知道有這樣的幾個問題,但他們都似乎有不同的方法來解決這個問題,沒有解決我的問題。Android IllegalStateException:試圖重新打開一個已經關閉的對象:SQLiteDatabase

我有我的主要活動工作得很好,加載數據庫和填充列表視圖。然後我打電話給第二個活動,當我嘗試加載列表視圖時出現問題。

我試過使用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; 
    } 

我希望你能幫助我。謝謝!

+0

什麼是'getReadDb()'?我認爲這很簡單。 – Sam 2013-04-04 22:28:53

+0

我已經用'getReadDb()'方法更新了這個問題。基本上是'instance.getReadableDatabase();'的sqlitedatabase – marimaf 2013-04-04 22:30:32

+0

這麼想,但想確定。你是否在該類的任何地方調用db.close()?或者你保存對'database = getReadDb()'的引用並調用'database.close()'? – Sam 2013-04-04 22:37:03

回答

2

我只是嘗試在parseNeabyBranches的類似方法中評論db.close,問題就解決了。然而,我沒有得到parseNearbyBranches()中db.close()的同樣錯誤,你能解釋我爲什麼嗎?

parseNearbyBranches()您創建一個單獨的SQLiteDatabase對象有:

SQLiteDatabase db = tpdbh.getWritableDatabase(); 

由於這是一個不同的對象比getReadDb()返回的一個,你可以(也應該)將其關閉。基本規則是每次您撥打getWritableDatabase()getReadableDatable()您必須有一個匹配的close()聲明。

+0

我的屏蔽是解決,雖然我也創建一個新的SQLiteDatabase在其他類似的。它與parseNearbyBranches非常相似,但具有不同的JSON對象和不同的查詢。 – marimaf 2013-04-04 23:06:09

+0

嗯,是你的問題中的代碼?我想看到他們兩個,也許這是你創建MySQLHelper的方式......它看起來像你正在試圖爲所有類使用一個實例。 – Sam 2013-04-05 14:26:38

+0

下面是其他方法的代碼:http://pastie.org/7328674 – marimaf 2013-04-05 15:06:30

相關問題