我的logcat顯示上述錯誤。.DatabaseObjectNotClosedException
然而,在Databasehelper類的所有方法中,我調用了db對象的close()方法。
這是db helper類的一些代碼。
public MyDatabaseAdapter(Context context,String databaseName){
super(context,databaseName,null,1);
this.mycontext=context;
try{
Log.v("Path",DB_PATH);
DB_NAME=databaseName;
Log.v("name",DB_NAME);
openDataBase();
}catch(Exception e)
{
Log.e("gezaaako",e.toString());
}
}
public SQLiteDatabase openDataBase() throws SQLException
{
String myPath=DB_PATH+DB_NAME;
Log.e("CheckPath",myPath);
if (myDataBase==null)
{
try {
createdatabase();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
myDataBase=SQLiteDatabase.openDatabase(myPath,null,SQLiteDatabase.OPEN_READWRITE);
}
return myDataBase;
}
public synchronized void close()
{
if(myDataBase!=null){
myDataBase.close();
}
super.close();
}
public long AddMeter(Meters meter){
SQLiteDatabase db=this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(SERIALNUMBER, meter.getSerialNumber().toString());
values.put(METERNUMBER, meter.getMeterNumber().toString());
values.put(DATEADDED, getDateTime());
long meterid=db.insert(TABLE_METERS, null, values);
db.close();
return meterid;
}
在方法AddMeter中,我顯式調用close方法。
下面是一個使用一個光標
public List<TenantMeterID> GetTenantMeterIds()
{
List<TenantMeterID> meterlist = new ArrayList<TenantMeterID>();
String selectQuery = " SELECT * FROM " +View_Tenant_Meter_id;
Log.e("LOG", selectQuery);
SQLiteDatabase db = this.getReadableDatabase();
Cursor c = db.rawQuery(selectQuery, null);
if(c.moveToFirst()){
do{
TenantMeterID t = new TenantMeterID();
t.set_id(Integer.parseInt(c.getString(c.getColumnIndex(KEY_ID))));
t.setFullName(c.getString(c.getColumnIndex(TENANT)));
meterlist.add(t);
} while(c.moveToNext());
}
c.close();
db.close();
return meterlist;
}
再者,我想,我明確地關閉遊標和db對象的方法。
這裏logcat的
01-31 10:42:53.843: D/dalvikvm(334): GC_CONCURRENT freed 179K, 50% free 2825K/5639K, external 734K/1032K, paused 4ms+8ms
01-31 10:42:53.903: E/Database(334): close() was never explicitly called on database 'data/data/com.example.metermanager/databases/meterapp.sqlite'
01-31 10:42:53.903: E/Database(334): android.database.sqlite.DatabaseObjectNotClosedException: Application did not close the cursor or database object that was opened here
01-31 10:42:53.903: E/Database(334): at android.database.sqlite.SQLiteDatabase.<init>(SQLiteDatabase.java:1847)
01-31 10:42:53.903: E/Database(334): at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:820)
01-31 10:42:53.903: E/Database(334): at meter.manager.helper.MyDatabaseAdapter.openDataBase(MyDatabaseAdapter.java:194)
01-31 10:42:53.903: E/Database(334): at meter.manager.helper.MyDatabaseAdapter.<init>(MyDatabaseAdapter.java:105)
01-31 10:42:53.903: E/Database(334): at com.example.metermanager.allTenants.<init>(allTenants.java:32)
01-31 10:42:53.903: E/Database(334): at com.example.metermanager.GetTeantList.onCreate(GetTeantList.java:21)
01-31 10:42:53.903: E/Database(334): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
01-31 10:42:53.903: E/Database(334): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
01-31 10:42:53.903: E/Database(334): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
01-31 10:42:53.903: E/Database(334): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
01-31 10:42:53.903: E/Database(334): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
01-31 10:42:53.903: E/Database(334): at android.os.Handler.dispatchMessage(Handler.java:99)
01-31 10:42:53.903: E/Database(334): at android.os.Looper.loop(Looper.java:123)
01-31 10:42:53.903: E/Database(334): at android.app.ActivityThread.main(ActivityThread.java:3683)
01-31 10:42:53.903: E/Database(334): at java.lang.reflect.Method.invokeNative(Native Method)
01-31 10:42:53.903: E/Database(334): at java.lang.reflect.Method.invoke(Method.java:507)
01-31 10:42:53.903: E/Database(334): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
01-31 10:42:53.903: E/Database(334): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
01-31 10:42:53.903: E/Database(334): at dalvik.system.NativeStart.main(Native Method)
這似乎表明,我必須expicitly關閉db對象在我使用DB輔助類的類。
是這樣嗎?
我一直在使用,只需在類中調用它。例如
public allTenants(Context context1) {
this.context=context1;
MyDatabaseAdapter db= new MyDatabaseAdapter(context1,DB_NAME);
readings=db.GetAllTenants();
}
在上面的例子中,如何顯式調用close方法?
讀取後= db.GetAllTenants();?
如果我的文章包含不相關的部分,我很抱歉!
羅納德
試過了,錯誤消失了。謝謝。 – user3079559