DatabaseNotClosed Exception - List View Scrolling
I have a list view populated by an adapter reading from SQLite database. When I scroll down in the list on screen I get the following error. I also get the same error when switching to another tab in the application (it is a tab based app).
close() was never explicitly called on database DatabaseNotClosedException : Application did not close the cursor or database object that was opened here.
Does anyone know why this is happening ? I have posted the main and adapter class files below.
Main Class :
public class Home extends ListActivity {
private DataBaseHelper myDbHelper;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.homeaz);
ListView yourList = (ListView) findViewById(android.R.id.list);
yourList.setOnItemClickListener(new OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
Log.e("onClick",""+arg3);
Intent i = new Intent(Home.this,DetailView.class);
i.putExtra(DataBaseHelper.ID, arg3);
// Activity returns an result if called with startActivityForResult
/**Home.this.finish();*/
startActivity(i);
}
});
myDbHelper = new DataBaseHelper(this);
try {
myDbHelper.createDataBase();
} catch (IOException ioe) {
throw new Error("Unable to create database");
}
try {
myDbHelper.openDataBase();
}catch(SQLException sqle){
throw sqle;
}
try {
Cursor cursor = getPlaces();
showPlaces(cursor);
}
finally {
myDbHelper.close();
}
}
private static String ORDER_BY = NAME + " ASC";
private Cursor getPlaces() {
SQLiteDatabase db = myDbHelper.getReadableDatabase();
Cursor cursor = db.query(TABLE_NAME, null, null, null, null, null, ORDER_BY);
startManagingCursor(cursor);
return cursor;
}
private void showPlaces(Cursor cursor) {
MyCustomAdapterClass adapter = new MyCustomAdapterClass(this,cursor, true); setListAdapter(adapter);
}
Adapter Class :
public class MyCustomAdapterClass extends CursorAdapter {
public MyCustomAdapterClass(Context context, Cursor c, boolean autoRequery) {
super(context, c, autoRequery);
// TODO Auto-generated constructor stub
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
return LayoutInflater.from(context).inflate(R.layout.item, parent, false);
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
((TextView) view.findViewById(R.id.name)).setText(cursor.getString(cursor.getColumnIndex("Name")));
((TextView) view.findViewById(R.id.description)).setText(cursor.getString(cursor.getColumnIndex("Area")));
((TextView) view.findViewById(R.id.address)).setText(cursor.getString(cursor.getColumnIndex("AddressLine1")));
switch (cursor.getInt(cursor.getColumnIndex("_id"))) {
case 1:
((ImageView) view.findViewById(R.id.Logo)).setImageResource(R.drawable.picture1);
break;
case 2:
((ImageView) view.findViewById(R.id.Logo)).setImageResource(R.drawable.picture2);
break;
case 3:
((ImageView) view.findViewById(R.id.Logo)).setImageResource(R.drawable.picture3);
break;
case 4:
((ImageView) view.findViewById(R.id.Logo)).setImageResource(R.drawable.picture4);
break;
case 5:
((ImageView) view.findViewById(R.id.Logo)).setImageResource(R.drawable.picture5);
break;
case 6:
((ImageView) view.findViewById(R.id.Logo)).setImageResource(R.drawable.picture6);
break;
case 7:
((ImageView) view.findViewById(R.id.Logo)).setImag开发者_StackOverflow中文版eResource(R.drawable.picture7);
break;
case 8:
((ImageView) view.findViewById(R.id.Logo)).setImageResource(R.drawable.picture8);
break;
case 9:
((ImageView) view.findViewById(R.id.Logo)).setImageResource(R.drawable.picture9);
break;
case 10:
((ImageView) view.findViewById(R.id.Logo)).setImageResource(R.drawable.picture10);
break;
case 11:
((ImageView) view.findViewById(R.id.Logo)).setImageResource(R.drawable.picture11);
break;
case 12:
((ImageView) view.findViewById(R.id.Logo)).setImageResource(R.drawable.picture12);
break;
case 13:
((ImageView) view.findViewById(R.id.Logo)).setImageResource(R.drawable.picture13);
break;
case 14:
((ImageView) view.findViewById(R.id.Logo)).setImageResource(R.drawable.picture14);
break;
case 15:
((ImageView) view.findViewById(R.id.Logo)).setImageResource(R.drawable.picture15);
break;
case 16:
((ImageView) view.findViewById(R.id.Logo)).setImageResource(R.drawable.picture16);
break;
case 17:
((ImageView) view.findViewById(R.id.Logo)).setImageResource(R.drawable.picture17);
break;
case 18:
((ImageView) view.findViewById(R.id.Logo)).setImageResource(R.drawable.picture18);
break;
case 19:
((ImageView) view.findViewById(R.id.Logo)).setImageResource(R.drawable.picture19);
break;
case 20:
Log :
09-10 18:44:25.495: INFO/ActivityManager(59): Starting activity: Intent { cmp=com.Beutelkind.Database/.DetailView (has extras) } 09-10 18:44:25.665: DEBUG/dalvikvm(415): GC_EXTERNAL_ALLOC freed 4966 objects / 284992 bytes in 66ms 09-10 18:44:26.284: DEBUG/dalvikvm(415): GC_EXTERNAL_ALLOC freed 300 objects / 14840 bytes in 56ms 09-10 18:44:27.435: INFO/ActivityManager(59): Displayed activity com.Beutelkind.Database/.DetailView: 1888 ms (total 1888 ms) 09-10 18:44:28.725: INFO/ActivityManager(59): Starting activity: Intent { cmp=com.Beutelkind.Database/.Main } 09-10 18:44:28.825: INFO/Database(415): sqlite returned: error code = 14, msg = cannot open file at source line 25467 09-10 18:44:28.825: ERROR/Database(415): sqlite3_open_v2("/mnt/sdcard/DatabaseC.sqlite", &handle, 1, NULL) failed 09-10 18:44:29.535: INFO/ActivityManager(59): Displayed activity com.Beutelkind.Database/.Main: 781 ms (total 781 ms) 09-10 18:44:32.405: DEBUG/dalvikvm(415): GC_EXTERNAL_ALLOC freed 2748 objects / 167024 bytes in 63ms 09-10 18:44:32.455: WARN/SQLiteCompiledSql(415): Releasing statement in a finalizer. Please ensure that you explicitly call close() on your cursor: SELECT DISTINCT _id, Name, Description, Area, Postcode, AddressLine1, OpeningTimes, NearestTube, Cos 09-10 18:44:32.455: WARN/SQLiteCompiledSql(415): android.database.sqlite.DatabaseObjectNotClosedException: Application did not close the cursor or database object that was opened here 09-10 18:44:32.455: WARN/SQLiteCompiledSql(415): at android.database.sqlite.SQLiteCompiledSql.(SQLiteCompiledSql.java:62) 09-10 18:44:32.455: WARN/SQLiteCompiledSql(415): at android.database.sqlite.SQLiteProgram.(SQLiteProgram.java:80) 09-10 18:44:32.455: WARN/SQLiteCompiledSql(415): at android.database.sqlite.SQLiteQuery.(SQLiteQuery.java:46) 09-10 18:44:32.455: WARN/SQLiteCompiledSql(415): at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:42) 09-10 18:44:32.455: WARN/SQLiteCompiledSql(415): at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1345) 09-10 18:44:32.455: WARN/SQLiteCompiledSql(415): at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1229) 09-10 18:44:32.455: WARN/SQLiteCompiledSql(415): at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1184) 09-10 18:44:32.455: WARN/SQLiteCompiledSql(415): at com.Beutelkind.Database.DetailView.populateFields(DetailView.java:79) 09-10 18:44:32.455: WARN/SQLiteCompiledSql(415): at com.Beutelkind.Database.DetailView.onCreate(DetailView.java:70) 09-10 18:44:32.455: WARN/SQLiteCompiledSql(415): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 09-10 18:44:32.455: WARN/SQLiteCompiledSql(415): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627) 09-10 18:44:32.455: WARN/SQLiteCompiledSql(415): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679) 09-10 18:44:32.455: WARN/SQLiteCompiledSql(415): at android.app.ActivityThread.access$2300(ActivityThread.java:125) 09-10 18:44:32.455: WARN/SQLiteCompiledSql(415): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033) 09-10 18:44:32.455: WARN/SQLiteCompiledSql(415): at android.os.Handler.dispatchMessage(Handler.java:99) 09-10 18:44:32.455: WARN/SQLiteCompiledSql(415): at android.os.Looper.loop(Looper.java:123) 09-10 18:44:32.455: WARN/SQLiteCompiledSql(415): at android.app.ActivityThread.main(ActivityThread.java:4627) 09-10 18:44:32.455: WARN/SQLiteCompiledSql(415): at java.lang.reflect.Method.invokeNative(Native Method) 09-10 18:44:32.455: WARN/SQLiteCompiledSql(415): at java.lang.reflect.Method.invoke(Method.java:521) 09-10 18:44:32.455: WARN/SQLiteCompiledSql(415): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 09-10 18:44:32.455: WARN/SQLiteCompiledSql(415): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 09-10 18:44:32.455: WARN/SQLiteCompiledSql(415): at dalvik.system.NativeStart.main(Native Method) 09-10 18:44:32.475: ERROR/Database(415): close() was never explicitly called on database '/data/data/com.Beutelkind.Database/databases/DatabaseC.sqlite' 09-10 18:44:32.475: ERROR/Database(415): android.database.sqlite.DatabaseObjectNotClosedException: Application did not close the cursor or database object that was opened here 09-10 18:44:32.475: ERROR/Database(415): at android.database.sqlite.SQLiteDatabase.(SQLiteDatabase.java:1810) 09-10 18:44:32.475: ERROR/Database(415): at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:817) 09-10 18:44:32.475: ERROR/Database(415): at android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:851) 09-10 18:44:32.475: ERROR/Database(415): at android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:844) 09-10 18:44:32.475: ERROR/Database(415): at android.app.ContextImpl.openOrCreateDatabase(ContextImpl.java:540) 09-10 18:44:32.475: ERROR/Database(415): at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:203) 09-10 18:44:32.475: ERROR/Database(415): at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:98) 09-10 18:44:32.475: ERROR/Database(415): at android.database.sqlite.SQLiteOpenHelper.getReadableDatabase(SQLiteOpenHelper.java:158) 09-10 18:44:32.475: ERROR/Database(415): at com.Beutelkind.Database.DetailView.populateFields(DetailView.java:77) 09-10 18:44:32.475: ERROR/Database(415): at com.Beutelkind.Database.DetailView.onCreate(DetailView.java:70) 09-10 18:44:32.475: ERROR/Database(415): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 09-10 18:44:32.475: ERROR/Database(415): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627) 09-10 18:44:32.475: ERROR/Database(415): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679) 09-10 18:44:32.475: ERROR/Database(415): at android.app.ActivityThread.access$2300(ActivityThread.java:125) 09-10 18:44:32.475: ERROR/Database(415): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033) 09-10 18:44:32.475: ERROR/Database(415): at android.os.Handler.dispatchMessage(Handler.java:99) 09-10 18:44:32.475: ERROR/Database(415): at android.os.Looper.loop(Looper.java:123) 09-10 18:44:32.475: ERROR/Database(415): at android.app.ActivityThread.main(ActivityThread.java:4627) 09-10 18:44:32.475: ERROR/Database(415): at java.lang.reflect.Method.invokeNative(Native Method) 09-10 18:44:32.475: ERROR/Database(415): at java.lang.reflect.Method.invoke(Method.java:521) 09-10 18:44:32.475: ERROR/Database(415): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 09-10 18:44:32.475: ERROR/Database(415): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 09-10 18:44:32.475: ERROR/Database(415): at dalvik.system.NativeStart.main(Native Method)
Easy one, you are not closing your database handles. For instance, you open a cursor in getPlaces but never close it when done using it, nor the db object from where it came.
精彩评论