[英]sqlite + listview + new Activity: close() was never explicitly called on database?
I have made an app that has 3 activities. 我制作了一个包含3个活动的应用程序。 In the fisrt activity(Import) i just import some values to a sqlite database. 在fisrt活动(导入)中,我只是将一些值导入sqlite数据库。
This is my DatabaseHelper class: 这是我的DatabaseHelper类:
public class DatabaseHelper_bp extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "bpDB";
private static final int DATABASE_VERSION = 1;
// Database creation sql statement
private static final String DATABASE_CREATE = "create table bp_import ( _id integer primary key, datetime text not null, systolic text not null, diastolic text not null, pulses text not null, notes text not null);";
public DatabaseHelper_bp(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
// Method is called during creation of the database
@Override
public void onCreate(SQLiteDatabase database) {
database.execSQL(DATABASE_CREATE);
}
// Method is called during an upgrade of the database,
@Override
public void onUpgrade(SQLiteDatabase database, int oldVersion,
int newVersion) {
Log.w(DatabaseHelper_bp.class.getName(),
"Upgrading database from version " + oldVersion + " to "
+ newVersion + ", which will destroy all old data");
database.execSQL("DROP TABLE IF EXISTS bp_import");
onCreate(database);
}
}
And my DAO class for my measures/values: 还有我的DAO类,用于度量/值:
public class BpDAO {
private DatabaseHelper_bp dbHelper;
private SQLiteDatabase database;
/**
* bp table related constants.
*/
public final static String bp_TABLE = "bp_import";
public final static String bp_ID = "_id";
public final static String bp_DT = "datetime";
public final static String bp_SYS = "systolic";
public final static String bp_DIA = "diastolic";
public final static String bp_PUL = "pulses";
public final static String bp_NOT = "notes";
/**
*
* @param context
*/
public BpDAO(Context context) {
dbHelper = new DatabaseHelper_bp(context);
database = dbHelper.getWritableDatabase();
}
/**
* \ Creates a new blood pressure measure
*
* @param datetime
* @param systolic
* @param diastolic
* @param pulses
* @param notes
* @return
*/
public long importBP(String datetime, String systolic, String diastolic,
String pulses, String notes) {
ContentValues values = new ContentValues();
values.put(bp_DT, datetime);
values.put(bp_SYS, systolic);
values.put(bp_DIA, diastolic);
values.put(bp_PUL, pulses);
values.put(bp_NOT, notes);
return database.insert(bp_TABLE, null, values);
}
public void close() {
database.close();
}
/**
* Fetch all bp
*
* @return
*/
public Cursor fetchAll_bp() {
Cursor mCursor = database.query(true, bp_TABLE, new String[] { bp_SYS,
bp_DIA, bp_DT, bp_ID }, null, null, null, null, null, null);
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
}
In the second activity(History) i have a List that is populated by the database,all ok 在第二个活动(历史)中,我有一个由数据库填充的列表,一切正常
Here is the code of 2 Activity(history): 这是2活动(历史)的代码:
public class HistoryActivity extends ListActivity {
private BpDAO dao;
private SimpleCursorAdapter dbAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
dao = new BpDAO(this);
Cursor bpList = dao.fetchAll_bp();
String[] from = new String[] { BpDAO.bp_SYS, BpDAO.bp_DIA, BpDAO.bp_DT };
int[] target = new int[] { R.id.bpSysHolder, R.id.bpDiaHolder,
R.id.bpDtHolder };
dbAdapter = new SimpleCursorAdapter(this, R.layout.history_bp, bpList,
from, target);
setListAdapter(dbAdapter);
}
@Override
public void onListItemClick(ListView l, View view, int position, long id) {
// TODO Auto-generated method stub
super.onListItemClick(l, view, position, id);
Log.d("BPT", "Selected bp id =" + id);
// log says that i have selected an item with id : 11
Cursor selectedBpDetails = (Cursor) l.getItemAtPosition(position);
String bp_DT = selectedBpDetails.getString(selectedBpDetails
.getColumnIndex(BpDAO.bp_DT));
String bp_SYS = selectedBpDetails.getString(selectedBpDetails
.getColumnIndex(BpDAO.bp_SYS));
String bp_DIA = selectedBpDetails.getString(selectedBpDetails
.getColumnIndex(BpDAO.bp_DIA));
String bp_PUL = selectedBpDetails.getString(selectedBpDetails
.getColumnIndex(BpDAO.bp_PUL));
String bp_NOT = selectedBpDetails.getString(selectedBpDetails
.getColumnIndex(BpDAO.bp_NOT));
Log.d("BPT", "Selected bp details = { date=" + bp_DT + ", systolic="
+ bp_SYS + ", diastolic=" + bp_DIA + ", pulses=" + bp_PUL
+ ", notes=" + bp_NOT + " }");
Intent intent = new Intent(HistoryActivity.this, FromHistory.class);
intent.putExtra("bp_SYS", bp_SYS);
intent.putExtra("bp_DIA", bp_DIA);
intent.putExtra("bp_DT", bp_DT);
intent.putExtra("bp_PUL", bp_PUL);
intent.putExtra("bp_NOT", bp_NOT);
startActivity(intent);
}
} }
When i click on a list item i start a new activity that i show all the infos of the measure from the database. 当我单击列表项时,我开始一个新活动,该活动显示数据库中该度量的所有信息。
But i have in logcat: 但我在logcat中:
1.close() was never explicitly called on database
2.android.database.sqlite.DatabaseObjectNotClosedException: Application did not close the cursor or database object that was opened here
3.E/System(561): java.lang.IllegalStateException: Don't have database lock!
I have seen some other questions but didn't manage to find how to close it for my situation. 我看到了其他一些问题,但没有找到如何针对我的情况关闭它的方法。
When you are done with cursor you need to close()
完成游标后,您需要close()
Example: 例:
selectedBpDetails.close();
Either you close the cursor directly after the fetching of your data by calling close()
on it, or you can override the onDestroy
method for your activity and then in the implementation of this method you close the cursor(s) that you have opened. 您可以在获取数据后直接通过调用close()
关闭游标,或者可以为活动重写onDestroy
方法,然后在该方法的实现中关闭已打开的游标。
public class HistoryActivity extends ListActivity {
// ...
Cursor bpList;
// ...
@Override
protected void onCreate(Bundle savedInstanceState) {
// ...
bpList = dao.fetchAll_bp();
// ...
}
@Override
protected void onDestroy() {
super.onDestroy();
bpList.close();
}
@Override
public void onListItemClick(ListView l, View view, int position, long id) {
// ...
Cursor selectedBpDetails = (Cursor) l.getItemAtPosition(position);
// ...
selectedBpDetails.close();
}
// ...
}
(You should close your Cursors when you are done with them as the others have pointed out, but...) (完成游标后,您应该关闭游标,如其他人所指出的,但是...)
The error is telling you that you need to close your SQLiteDatabase. 该错误告诉您需要关闭SQLiteDatabase。 Add this method to your BpDAO
class: 将此方法添加到您的BpDAO
类中:
public void close() {
database.close();
}
And whenever you create a new BpDAO
object in any Activity you need to call close()
, you can do this as soon as you are done or in onDestroy()
: 每当在任何Activity中创建新的BpDAO
对象时,都需要调用close()
,就可以在完成后或在onDestroy()
立即执行此操作:
@Override
protected void onDestroy() {
super.onDestroy();
dao.close();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.