我写了一个类,允许我的用户更新他们的约会细节。
在'onCreate'方法中,我填充了许多editTexts,并在时间选择器和我的数据库中的切换按钮上设置了日期选择器。填充这些项目工作正常。我的问题是,只要我调整数据贴纸的预设“日期”值,我就会得到:
'在终结器中发布声明'错误。
这导致我的try catch被触发并且编辑无法更新。 这是完整的LogCat错误:
02-07 17:27:03.741: W/SQLiteCompiledSql(278): Releasing statement in a finalizer. Please ensure that you explicitly call close() on your cursor: SELECT _id, app_name, app_type, app_time, app_date, app_comments FROM appointmentsTable WHERE _id=2
02-07 17:27:03.741: W/SQLiteCompiledSql(278): android.database.sqlite.DatabaseObjectNotClosedException: Application did not close the cursor or database object that was opened here
02-07 17:27:03.741: W/SQLiteCompiledSql(278): at android.database.sqlite.SQLiteCompiledSql.<init>(SQLiteCompiledSql.java:62)
02-07 17:27:03.741: W/SQLiteCompiledSql(278): at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:80)
02-07 17:27:03.741: W/SQLiteCompiledSql(278): at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:46)
02-07 17:27:03.741: W/SQLiteCompiledSql(278): at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:42)
02-07 17:27:03.741: W/SQLiteCompiledSql(278): at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1345)
02-07 17:27:03.741: W/SQLiteCompiledSql(278): at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1229)
02-07 17:27:03.741: W/SQLiteCompiledSql(278): at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1184)
02-07 17:27:03.741: W/SQLiteCompiledSql(278): at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1264)
02-07 17:27:03.741: W/SQLiteCompiledSql(278): at com.example.flybase2.DBHandlerApp.getComments(DBHandlerApp.java:355)
02-07 17:27:03.741: W/SQLiteCompiledSql(278): at com.example.flybase2.AppointmentEditChanges.onCreate(AppointmentEditChanges.java:178)
02-07 17:27:03.741: W/SQLiteCompiledSql(278): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
02-07 17:27:03.741: W/SQLiteCompiledSql(278): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
02-07 17:27:03.741: W/SQLiteCompiledSql(278): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
02-07 17:27:03.741: W/SQLiteCompiledSql(278): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
02-07 17:27:03.741: W/SQLiteCompiledSql(278): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
02-07 17:27:03.741: W/SQLiteCompiledSql(278): at android.os.Handler.dispatchMessage(Handler.java:99)
02-07 17:27:03.741: W/SQLiteCompiledSql(278): at android.os.Looper.loop(Looper.java:123)
02-07 17:27:03.741: W/SQLiteCompiledSql(278): at android.app.ActivityThread.main(ActivityThread.java:4627)
02-07 17:27:03.741: W/SQLiteCompiledSql(278): at java.lang.reflect.Method.invokeNative(Native Method)
02-07 17:27:03.741: W/SQLiteCompiledSql(278): at java.lang.reflect.Method.invoke(Method.java:521)
02-07 17:27:03.741: W/SQLiteCompiledSql(278): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
02-07 17:27:03.741: W/SQLiteCompiledSql(278): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
02-07 17:27:03.741: W/SQLiteCompiledSql(278): at dalvik.system.NativeStart.main(Native Method)
02-07 17:27:03.772: E/Database(278): close() was never explicitly called on database '/data/data/com.example.flybase2/databases/app_name'
02-07 17:27:03.772: E/Database(278): android.database.sqlite.DatabaseObjectNotClosedException: Application did not close the cursor or database object that was opened here
02-07 17:27:03.772: E/Database(278): at android.database.sqlite.SQLiteDatabase.<init>(SQLiteDatabase.java:1810)
02-07 17:27:03.772: E/Database(278): at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:817)
02-07 17:27:03.772: E/Database(278): at android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:851)
02-07 17:27:03.772: E/Database(278): at android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:844)
02-07 17:27:03.772: E/Database(278): at android.app.ContextImpl.openOrCreateDatabase(ContextImpl.java:540)
02-07 17:27:03.772: E/Database(278): at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:203)
02-07 17:27:03.772: E/Database(278): at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:98)
02-07 17:27:03.772: E/Database(278): at com.example.flybase2.DBHandlerApp.open(DBHandlerApp.java:157)
02-07 17:27:03.772: E/Database(278): at com.example.flybase2.AppointmentEditChanges.onCreate(AppointmentEditChanges.java:176)
02-07 17:27:03.772: E/Database(278): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
02-07 17:27:03.772: E/Database(278): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
02-07 17:27:03.772: E/Database(278): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
02-07 17:27:03.772: E/Database(278): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
02-07 17:27:03.772: E/Database(278): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
02-07 17:27:03.772: E/Database(278): at android.os.Handler.dispatchMessage(Handler.java:99)
02-07 17:27:03.772: E/Database(278): at android.os.Looper.loop(Looper.java:123)
02-07 17:27:03.772: E/Database(278): at android.app.ActivityThread.main(ActivityThread.java:4627)
02-07 17:27:03.772: E/Database(278): at java.lang.reflect.Method.invokeNative(Native Method)
02-07 17:27:03.772: E/Database(278): at java.lang.reflect.Method.invoke(Method.java:521)
02-07 17:27:03.772: E/Database(278): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
02-07 17:27:03.772: E/Database(278): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
02-07 17:27:03.772: E/Database(278): at dalvik.system.NativeStart.main(Native Method)
02-07 17:27:03.811: W/SQLiteCompiledSql(278): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
这是我目前的'AppointmentEditChanges'课程:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.editappointment);
editAppointName = (EditText) findViewById(R.id.inputNewAppName);
editTime = (Spinner) findViewById(R.id.spinner1);
editTimePicker = (TimePicker) findViewById(R.id.timePickerEdit);
editDatePicker = (DatePicker) findViewById(R.id.datePickerEdit);
editCommentsApp = (EditText) findViewById(R.id.inputNewComm);
editToggle = (ToggleButton) findViewById(R.id.toggleButton1);
editUpdateApp = (Button) findViewById(R.id.btnChangesApp);
editDelApp = (Button) findViewById(R.id.btnDeleteApp);
Bundle extras = getIntent().getExtras();
if (extras != null) {
passedID = extras.getLong("passedID");
}
DBHandlerApp getName = new DBHandlerApp(this, null, null);
getName.open();
String nameReturned = getName.getNameEditForAppointment(passedID);
String typeReturned = getName.getAppointTypeEditForAppointment(passedID);
editAppointName.setText(nameReturned);
List<String> list = new ArrayList<String>();
list.add("-");
list.add("Medical");
list.add("Business");
list.add("Family");
list.add("Other");
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, list);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
editTime.setAdapter(dataAdapter);
for(int a = 0; a < list.size(); a ++)
{
if(typeReturned.equals(list.get(a)))
{
editTime.setSelection(a);
}
}
DBHandlerApp timeToEdit= new DBHandlerApp(this, null, null);
timeToEdit.open();
String returnedTime = timeToEdit.getTime(passedID);
SimpleDateFormat newFormat = new SimpleDateFormat("HH:mm");
try
{
cake = newFormat.parse(returnedTime);
} catch (ParseException e)
{
e.printStackTrace();
}
Calendar cal = Calendar.getInstance();
cal.setTime(cake);
editTimePicker.setCurrentHour(cal.get( Calendar.HOUR_OF_DAY ));
editTimePicker.setCurrentMinute(cal.get( Calendar.MINUTE ));
DBHandlerApp dateToEdit= new DBHandlerApp(this, null, null);
dateToEdit.open();
String returnedDate = dateToEdit.dateToEdit(passedID);
dateToEdit.close();
SimpleDateFormat newFormatDate = new SimpleDateFormat("dd-MMM-yyyy");
try
{
dateToEditApp = newFormatDate.parse(returnedDate);
} catch (ParseException e)
{
e.printStackTrace();
}
Calendar calDate = Calendar.getInstance();
calDate.setTime(dateToEditApp);
int year = calDate.get(Calendar.YEAR);
int monthOfYear = calDate.get(Calendar.MONTH);
int dayOfMonth = calDate.get(Calendar.DAY_OF_MONTH);
editDatePicker.init(year, monthOfYear, dayOfMonth, null);
DBHandlerApp commentsApp = new DBHandlerApp(this, null, null);
commentsApp.open();
String returnedComment = commentsApp.getComments(passedID);
editCommentsApp.setText(returnedComment);
typeToSet = editTime.getSelectedItem().toString();
DBHandlerApp editAlarm = new DBHandlerApp(this, null, null);
editAlarm.open();
Integer alarmEditResult = editAlarm.checkAlarmStatus(passedID);
if(alarmEditResult == 1)
{
setTog = true;
editToggle.setChecked(setTog);
}
else
{
setTog = false;
editToggle.setChecked(setTog);
}
editUpdateApp.setOnClickListener(this);
editDelApp.setOnClickListener(this);
editToggle.setChecked(setTog);
editToggle.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if (editToggle.isChecked()) {
setTog = true;
editToggle.setChecked(setTog);
} else {
setTog = false;
editToggle.setChecked(setTog);
}
}
});
}
@Override
public void onClick(View editChoice) {
switch(editChoice.getId()){
case (R.id.btnChangesApp):
if(setTog != false)
try{
Integer dobMonth = editDatePicker.getMonth();
Integer dobYear = editDatePicker.getYear();
Integer dobDate = editDatePicker.getDayOfMonth();
Integer dobHour = editTimePicker.getCurrentHour();
Integer dobMinute = editTimePicker.getCurrentMinute();
String timeToSet;
Format formatter;
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, setTime.getCurrentHour());
calendar.set(Calendar.MINUTE, setTime.getCurrentMinute());
formatter = new SimpleDateFormat("HH:mm");
timeToSet = formatter.format(calendar.getTime());
////////////////////////////////////////////////
String dateToSet;
Format formatterDate;
Calendar calendarDate = Calendar.getInstance();
calendarDate.set(Calendar.YEAR, editDatePicker.getYear());
calendarDate.set(Calendar.MONTH, editDatePicker.getMonth());
calendarDate.set(Calendar.DAY_OF_MONTH, editDatePicker.getDayOfMonth());
formatterDate = new SimpleDateFormat("dd-MMM-yyyy");
dateToSet = formatterDate.format(calendar.getTime());
//////////////////////////////////////////////////
Date setDate = new Date(dobYear - 1900, dobMonth, dobDate);
//Time timeToSet = new Time();
//timeToSet.set(0, dobMinute, dobHour);
alarmToSet = true;
DBHandlerApp updateApp = new DBHandlerApp(this, null, null);
updateApp.open();
updateApp.updateAppDetails(passedID, updateNameAppoint, updateCommsAppoint, timeToSet, dateToSet, typeToSet, alarmToSet);
AlarmManager dateSet = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(this, DateAlarm.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
dateSet.set(AlarmManager.RTC_WAKEUP, setDate.getTime(), pendingIntent);
}
catch (Exception e)
{
check = false;
Dialog d = new Dialog(this);
d.setTitle("Appointment Failed To Be Updated 1");
TextView txt = new TextView(this);
txt.setText("Fail");
d.setContentView(txt);
d.show();
}
finally
{
if(check = true);
{
Dialog e = new Dialog(this);
e.setTitle("Appointment Has Been Updated.");
TextView txt = new TextView(this);
txt.setText("Success");
e.setContentView(txt);
e.show();
}
}
else if(setTog == false)
{
alarmToSet = false;
try
{
DBHandlerApp createAppFalse = new DBHandlerApp(this, null, null);
Integer dobMonth = editDatePicker.getMonth();
Integer dobYear = editDatePicker.getYear();
Integer dobDate = editDatePicker.getDayOfMonth();
Integer dobHour = editTimePicker.getCurrentHour();
Integer dobMinute = editTimePicker.getCurrentMinute();
Date setDate = new Date(dobYear - 1900, dobMonth, dobDate);
//Time timeToSet = new Time();
//timeToSet.set(0, dobMinute, dobHour);
String timeToSet;
Format formatter;
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, setTime.getCurrentHour());
calendar.set(Calendar.MINUTE, setTime.getCurrentMinute());
formatter = new SimpleDateFormat("HH:mm");
timeToSet = formatter.format(calendar.getTime());
////////////////////////////////////////////////
String dateToSeta;
Format formatterDatea;
Calendar calendarDate = Calendar.getInstance();
calendarDate.set(Calendar.YEAR, editDatePicker.getYear());
calendarDate.set(Calendar.MONTH, editDatePicker.getMonth());
calendarDate.set(Calendar.DAY_OF_MONTH, editDatePicker.getDayOfMonth());
formatterDatea = new SimpleDateFormat("dd-MMM-yyyy");
dateToSeta = formatterDatea.format(calendar.getTime());
//////////////////////////////////////////////////
DBHandlerApp updateApp = new DBHandlerApp(this, null, null);
updateApp.open();
updateApp.updateAppDetails(passedID, updateNameAppoint, typeToSet, timeToSet, dateToSeta, updateCommsAppoint, alarmToSet);
}
catch (Exception e)
{
check = false;
Dialog d = new Dialog(this);
d.setTitle("Appointment Failed To Be Added 2");
TextView txt = new TextView(this);
txt.setText("Fail");
d.setContentView(txt);
d.show();
}
finally
{
if(check = true);
{
Dialog e = new Dialog(this);
e.setTitle("New Appointment Added.");
TextView txt = new TextView(this);
txt.setText("Success");
e.setContentView(txt);
e.show();
}
finish();
}
}
break;
case (R.id.btnDeleteApp):
DBHandlerApp deleteApp = new DBHandlerApp(this, null, null);
deleteApp.open();
deleteApp.deleteAppointment(passedID);
deleteApp.close();
break;
}
}
}
这些是我在数据库Handler类中使用的方法。据我所见,我正确关闭游标?
public String getNameEditForAppointment(Long passedID) {
String [] columns = new String[]{KEY_ROWAPPID, KEY_NAMEAPP, KEY_TYPEAPP, KEY_TIMEAPP, KEY_DATEAPP, KEY_COMMENTAPP};
Cursor c = ourDatabase.query(DATABASE_TABLEAPP, columns, KEY_ROWAPPID + "=" + passedID, null, null, null, null);
if(c != null)
{
if(c.getCount() > 0 && c.moveToFirst())
{
c.moveToFirst();
String name = c.getString(1);
c.close();
return name;
}
}
c.close();
return null;
}
public String getAppointTypeEditForAppointment(Long passedID) {
String [] columns = new String[]{KEY_ROWAPPID, KEY_NAMEAPP, KEY_TYPEAPP, KEY_TIMEAPP, KEY_DATEAPP, KEY_COMMENTAPP};
Cursor c = ourDatabase.query(DATABASE_TABLEAPP, columns, KEY_ROWAPPID + "=" + passedID, null, null, null, null);
if(c != null)
{
if(c.getCount() > 0 && c.moveToFirst())
{
c.moveToFirst();
String name = c.getString(2);
c.close();
return name;
}
}
c.close();
return null;
}
public String getTime(Long passedID) {
String [] columns = new String[]{KEY_ROWAPPID, KEY_NAMEAPP, KEY_TYPEAPP, KEY_TIMEAPP, KEY_DATEAPP, KEY_COMMENTAPP};
Cursor c = ourDatabase.query(DATABASE_TABLEAPP, columns, KEY_ROWAPPID + "=" + passedID, null, null, null, null);
if(c != null)
{
if(c.getCount() > 0 && c.moveToFirst())
{
c.moveToFirst();
String name = c.getString(3);
c.close();
return name;
}
}
c.close();
return null;
}
public String dateToEdit(Long passedID) {
String [] columns = new String[]{KEY_ROWAPPID, KEY_NAMEAPP, KEY_TYPEAPP, KEY_TIMEAPP, KEY_DATEAPP, KEY_COMMENTAPP};
Cursor c = ourDatabase.query(DATABASE_TABLEAPP, columns, KEY_ROWAPPID + "=" + passedID, null, null, null, null);
if(c != null)
{
if(c.getCount() > 0 && c.moveToFirst())
{
c.moveToFirst();
String date = c.getString(4);
c.close();
return date;
}
}
c.close();
return null;
}
public String getComments(Long passedID) {
String [] columns = new String[]{KEY_ROWAPPID, KEY_NAMEAPP, KEY_TYPEAPP, KEY_TIMEAPP, KEY_DATEAPP, KEY_COMMENTAPP};
Cursor c = ourDatabase.query(DATABASE_TABLEAPP, columns, KEY_ROWAPPID + "=" + passedID, null, null, null, null);
if(c != null && c.getCount() > 0)
{
c.moveToFirst();
String comment = c.getString(5);
if(comment.equals(""))
{
String noComm = "No Set Comment.";
c.close();
return noComm;
}
else
{
c.close();
return comment;
}
}
c.close();
return null;
}
public Integer checkAlarmStatus(Long passedID) {
String [] columns = new String[]{KEY_ROWAPPID, KEY_NAMEAPP, KEY_TYPEAPP, KEY_TIMEAPP, KEY_DATEAPP, KEY_COMMENTAPP, KEY_ALARM};
Cursor c = ourDatabase.query(DATABASE_TABLEAPP, columns, KEY_ROWAPPID + "=" + passedID, null, null, null, null);
if(c != null)
{
if(c.getCount() > 0 && c.moveToFirst())
{
c.moveToFirst();
int res = c.getInt(6);
c.close();
return res;
}
}
c.close();
return null;
}
答案 0 :(得分:0)
你好像正在关闭游标。但是,我没有看到你关闭数据库对象的位置。在完成每个对象后,请尝试关闭DBHandlerApp
个对象。