错误:尝试访问关闭的光标窗口

时间:2013-10-05 11:54:07

标签: android

您好我尝试将SQLite数据库中的数据加载到微调器中,因此我尝试访问已关闭的CursorWindow时出现此错误。 我有这些课: CategoryBDD.java     包com.example.mydrugstore;

public class CategoryBDD {

private SQLiteDatabase mdb;
private BasepharmaSQLiteHelper mabasepharma;

public CategoryBDD (Context context)
{
mabasepharma = new BasepharmaSQLiteHelper(context, Parambdd.DB_NAME, null,  Parambdd.SCHEMA_VERSION);
}

/** ouvre la base de données en écriture */
public void open(){
      //on ouvre la BDD en écriture
      mdb = mabasepharma.getWritableDatabase();
}

/** ferme la base de données */
public void close(){
      //on ferme l'accès à la BDD
      mdb.close();
}


public Category insertcategory( Category c)
{
 ContentValues valeur = new ContentValues();
    valeur.put(Parambdd.CATEGORY_NAME, c.getName());
    long insertId= mdb.insert(Parambdd.TABLECATEGORY, null, valeur);
    Cursor cursor = mdb.query(Parambdd.TABLECATEGORY,
            new String[] {Parambdd.CATEGORY_NAME}, Parambdd.CATEGORY_ID + " = " + insertId, null,
            null, null, null);
        cursor.moveToFirst();
        Category newcategory = cursorToCategory(cursor);
        cursor.close();
        return newcategory;
}

public int updateCategory(Category ctgr){
    ContentValues values = new ContentValues();
    values.put(Parambdd.CATEGORY_NAME, ctgr.getName());
    return mdb.update(Parambdd.TABLECATEGORY, values, Parambdd.CATEGORY_NAME + " = " +ctgr.getName(), null);
}


public void deletecategory ( Category c)
{
  long id = c.getId_category();
    mdb.delete(Parambdd.TABLECATEGORY,Parambdd.CATEGORY_ID+" = "+id, null);
}

public List<String> getAllCategorys() {
    List<String> categorys = new ArrayList<String>();
    Cursor cursor = mdb.query(Parambdd.TABLECATEGORY, new String[] {Parambdd.CATEGORY_NAME}, null, null, null, null, null);
    cursor.moveToFirst();
    if (cursorToCategory(cursor) == null)
    {
        return null;
    }
    else
    {
    while (!cursor.isAfterLast()) {
      Category c = cursorToCategory(cursor);
      categorys.add(c.getName());
      cursor.moveToNext();
    }
    // Make sure to close the cursor
    cursor.close();
    return categorys;
    }
  }

 private Category cursorToCategory(Cursor c){
     if (c.getCount() == 0)
           return null;
     c.moveToFirst();
     Category ctgr = new Category(c.getString(0));

 //    ctgr.setId_category(c.getLong(0));
   //  ctgr.setName(c.getString(1));

     c.close();
     return ctgr;
}
}

Main.java:     public class Main扩展Activity {

 Spinner spinner;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.adddrug);
    spinner = (Spinner) findViewById(R.id.spinner1);
    loadSpinnerData();


}


 private void loadSpinnerData() {
        // database handler
        CategoryBDD catdb = new CategoryBDD(getApplicationContext());
        catdb.open();

        Category c = new Category("toto");  //test insert category "toto"
        catdb.insertcategory(c);
     //   catdb.open();
        // Spinner Drop down elements
        List<String> categoryname = catdb.getAllCategorys();
        for (int i = 0;i < categoryname.size();i++)
        {
            Log.w("Main", categoryname.get(i));
        }

        // Creating adapter for spinner
        ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_spinner_item, categoryname);

        // Drop down layout style - list view with radio button
          dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

        // attaching data adapter to spinner
        spinner.setAdapter(dataAdapter);
    }
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}
}

任何人都可以帮助我吗?

3 个答案:

答案 0 :(得分:1)

我通过在方法loadspinnerdata中删除“catdb.open()”并在这些方法“insertcategory and getallcategorys”中关闭对象sqldatabase来解决我的问题 感谢

答案 1 :(得分:0)

// replace this code
private Category cursorToCategory(Cursor c){
     if (c.getCount() == 0)
           return null;
     c.moveToFirst();
     Category ctgr = new Category(c.getString(0));

 //    ctgr.setId_category(c.getLong(0));
   //  ctgr.setName(c.getString(1));

     return ctgr;
}

答案 2 :(得分:0)

在getAllCategorys()方法中,您正在检查

if(cursorToCategory(cursor) == null){ }

在cursorToCategory()中你正在关闭光标,你再次通过传递相同的光标来调用相同的方法

Category c = cursorToCategory(cursor);

所以不要在cursorToCategory()方法中关闭游标。