2

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

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

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;
    }

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

4

1 回答 1

2

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

parseNearbyBranches()您创建一个单独的 SQLiteDatabase 对象时:

SQLiteDatabase db = tpdbh.getWritableDatabase();

由于这是与返回的对象不同的对象getReadDb(),因此您可以(并且应该)关闭它。基本规则是每次你打电话getWritableDatabase()getReadableDatable()你必须有一个匹配的close()语句。

于 2013-04-04T22:56:13.607 回答