안드로이드 개발 질문/답변
(글 수 45,052)
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tab2);
db = new DBAdapter(this, sql_myfavorite, myfavorite); // this.getApplicationContext()
db.open();
String columns[] = { "name", "code" };
Cursor cursor = db.selectTable(columns, null, null, null, null, null);
ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
cursor.moveToFirst();
while (cursor.moveToNext()) {
HashMap<String, String> map = new HashMap<String, String>();
map.put("name", cursor.getString(0));
map.put("code", cursor.getString(1));
list.add(map);
}
SimpleAdapter sap = new SimpleAdapter(this, list,
android.R.layout.simple_list_item_1, new String[] { "name", "code" },
new int[] { android.R.id.text1, android.R.id.text2 });
lv = (ListView) findViewById(R.id.tab2_lView);
lv.setAdapter(sap);
cursor.close();
db.close();
}
05-23 14:36:34.297: ERROR/Database(3784): android.database.sqlite.DatabaseObjectNotClosedException: Application did not close the cursor or database object that was opened here
위와같은 오류가 납니다. db.close(); 도 해주고 cursor.close(); 도 해줬는데 왜이러는지 모르겠네요 ㅠ
public DBAdapter open() throws SQLException {
// 외부에서 db를 사용하겠다고 요청이 들어오면 Helper를 이용해서 db를 open하고
// 자신의 클래스를 리턴
mHelper = new DatabaseHelper(mCxt); // SQLiteOpenHelper에게 context를 넘겨준다.
mDb = mHelper.getWritableDatabase();
return this;
}
오류나는곳은 mDb = mHelper.getWritableDatabase(); 이 부분에서 오류가 납니다.
테이블도 생성이 되질 않네요...




일단
커서.클로즈 시점이 늦은거같습니다
while문 바로 아래서 해주시는게
좋을거 같구요. 디비를 사용하는 경우 트라이 캐치 파이널로 묶어서 커서를 파이널에서 클로즈
해주셔도 좋습니다(일케 안하시면 추후 디비쓰는 액티비티에서 백키로 이동하면 거의 저 에러뜹니다)
글구
디비는 굳이 클로즈 안하셔도 됩니다