- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我有这个使用 sqlite 的数据库,我在关闭游标时遇到问题它说 Application did not close the cursor or database object that was opened here
here's the logcat
10-18 08:40:56.354: E/Cursor(331): android.database.sqlite.DatabaseObjectNotClosedException: Application did not close the cursor or database object that was opened here
10-18 08:40:56.354: E/Cursor(331): at android.database.sqlite.SQLiteCursor.<init>(SQLiteCursor.java:210)
10-18 08:40:56.354: E/Cursor(331): at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:53)
10-18 08:40:56.354: E/Cursor(331): at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1345)
10-18 08:40:56.354: E/Cursor(331): at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1229)
10-18 08:40:56.354: E/Cursor(331): at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1184)
10-18 08:40:56.354: E/Cursor(331): at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1264)
10-18 08:40:56.354: E/Cursor(331): at standard.internet.marketing.mymovingfriend.SQLHandler.checkMove(SQLHandler.java:1094)
10-18 08:40:56.354: E/Cursor(331): at standard.internet.marketing.mymovingfriend.ListMovingNames$3.onKey(ListMovingNames.java:98)
10-18 08:40:56.354: E/Cursor(331): at android.view.View.dispatchKeyEvent(View.java:3735)
10-18 08:40:56.354: E/Cursor(331): at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:788)
10-18 08:40:56.354: E/Cursor(331): at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:788)
10-18 08:40:56.354: E/Cursor(331): at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:788)
10-18 08:40:56.354: E/Cursor(331): at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:788)
10-18 08:40:56.354: E/Cursor(331): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchKeyEvent(PhoneWindow.java:1667)
10-18 08:40:56.354: E/Cursor(331): at com.android.internal.policy.impl.PhoneWindow.superDispatchKeyEvent(PhoneWindow.java:1102)
10-18 08:40:56.354: E/Cursor(331): at android.app.Activity.dispatchKeyEvent(Activity.java:2063)
10-18 08:40:56.354: E/Cursor(331): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:1643)
10-18 08:40:56.354: E/Cursor(331): at android.view.ViewRoot.deliverKeyEventToViewHierarchy(ViewRoot.java:2471)
10-18 08:40:56.354: E/Cursor(331): at android.view.ViewRoot.handleFinishedEvent(ViewRoot.java:2441)
10-18 08:40:56.354: E/Cursor(331): at android.view.ViewRoot.handleMessage(ViewRoot.java:1735)
10-18 08:40:56.354: E/Cursor(331): at android.os.Handler.dispatchMessage(Handler.java:99)
10-18 08:40:56.354: E/Cursor(331): at android.os.Looper.loop(Looper.java:123)
10-18 08:40:56.354: E/Cursor(331): at android.app.ActivityThread.main(ActivityThread.java:4627)
10-18 08:40:56.354: E/Cursor(331): at java.lang.reflect.Method.invokeNative(Native Method)
10-18 08:40:56.354: E/Cursor(331): at java.lang.reflect.Method.invoke(Method.java:521)
10-18 08:40:56.354: E/Cursor(331): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
10-18 08:40:56.354: E/Cursor(331): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
10-18 08:40:56.354: E/Cursor(331): at dalvik.system.NativeStart.main(Native Method)
这个问题已经困扰我 3 天了。
这里是一些相关的代码:
public class SQLHandler {
public static final String KEY_MOVENAME = "movename";
public static final String KEY_ID1 = "_id";
public static final String KEY_ID5 = "_id";
public static final String KEY_MOVEDATE = "movedate";
public static final String KEY_TOTALMOVEWEIGHT = "totalmoveweight";
public static final String KEY_TOTALITEM = "totalitem";
private static final String DATABASE_NAME = "mymovingfriend";
private static final int DATABASE_VERSION = 1;
public static final String KEY_LISTITEMNAME = "listitemname";
public static final String KEY_LISTITEMWEIGHT = "listitemweight";
public static final String KEY_LISTITEMROOM = "listitemroom";
private static final String DATABASE_TABLE1 = "movingname";
private static final String DATABASE_TABLE5 = "listitem";
public static final String CREATE_TABLE_1 = "CREATE TABLE " + DATABASE_TABLE1 + " (" +
KEY_ID1 + " INTEGER PRIMARY KEY AUTOINCREMENT," +
KEY_MOVEDATE + " TEXT NOT NULL, " +
KEY_TOTALMOVEWEIGHT + " TEXT NOT NULL, " +
KEY_TOTALITEM + " INTEGER NOT NULL, " +
KEY_MOVENAME + " TEXT NOT NULL);";
public static final String CREATE_TABLE_2 = "CREATE TABLE " + DATABASE_TABLE2 + " (" +
KEY_ID2 + " INTEGER PRIMARY KEY AUTOINCREMENT," +
KEY_ROOMMOVEHOLDER + " TEXT NOT NULL, " +
KEY_ROOMWEIGHT + " TEXT NOT NULL, " +
KEY_ROOM + " TEXT NOT NULL);";
public static final String CREATE_TABLE_5 = "CREATE TABLE " + DATABASE_TABLE5 + " (" +
KEY_ID5 + " INTEGER PRIMARY KEY AUTOINCREMENT," +
KEY_LISTITEMNAME + " TEXT NOT NULL, " +
KEY_LISTITEMWEIGHT + " TEXT NOT NULL, " +
KEY_LISTITEMROOM + " TEXT NOT NULL);";
private DbHelper ourHelper;
private final Context ourContext;
private SQLiteDatabase ourDatabase;
private static class DbHelper extends SQLiteOpenHelper{
public DbHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL(CREATE_TABLE_1);
db.execSQL(CREATE_TABLE_2);
db.execSQL(CREATE_TABLE_3);
db.execSQL(CREATE_TABLE_4);
db.execSQL(CREATE_TABLE_5);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldversion, int newversion) {
// TODO Auto-generated method stub
db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE1);
db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE2);
db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE3);
db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE4);
db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE5);
onCreate(db);
}
}
public SQLHandler(Context c){
ourContext = c;
}
public SQLHandler open() throws SQLException{
ourHelper = new DbHelper(ourContext);
ourDatabase = ourHelper.getWritableDatabase();
return this;
}
public void close(){
ourDatabase.close();
ourHelper.close();
}
public long createMove(String smovename){
ContentValues cv = new ContentValues();
cv.put(KEY_MOVENAME, smovename);
cv.put(KEY_MOVEDATE, "Not yet set");
cv.put(KEY_TOTALMOVEWEIGHT, "0");
cv.put(KEY_TOTALITEM, 0);
return ourDatabase.insert(DATABASE_TABLE1, null, cv);
}
public void createList(){
String[] sroom = new String[]{"Kitchen", "Bedroom", "Dinning Room"};
String[] sitem = new String[]{"Dishwasher", "Bed", "Table"};
String[] sweight = new String[]{"40", "25", "15"};
for (int i = 0; i < sroom.length; i++) {
cv.put(KEY_LISTITEMROOM, sroom[i]);
cv.put(KEY_LISTITEMNAME, sitem[i]);
cv.put(KEY_LISTITEMWEIGHT, sweight[i]);
ourDatabase.insert(DATABASE_TABLE5, null, cv);
}
}
public void setMoveDate(String smovedate, String smovename){
ContentValues cv = new ContentValues();
cv.put(KEY_MOVEDATE, smovedate);
ourDatabase.update(DATABASE_TABLE1, cv, KEY_MOVENAME + "='" + smovename + "'", null);
}
public void setMoveWeight(String smoveweight, String smovename){
ContentValues cv = new ContentValues();
cv.put(KEY_TOTALMOVEWEIGHT, smoveweight);
ourDatabase.update(DATABASE_TABLE1, cv, KEY_MOVENAME + "='" + smovename + "'", null);
}
public void setTotalItem(String smovename, int imoveitem){
ContentValues cv = new ContentValues();
cv.put(KEY_TOTALITEM, imoveitem);
ourDatabase.update(DATABASE_TABLE1, cv, KEY_MOVENAME + "='" + smovename + "'", null);
}
public void renameRoom(String movename, String roomname, String currentroom){
ContentValues cv = new ContentValues();
cv.put(KEY_ROOM, roomname);
ourDatabase.update(DATABASE_TABLE2, cv, KEY_ROOMMOVEHOLDER + "='" + movename + "'" + " AND " + KEY_ROOM + "='" + currentroom + "'", null);
}
public void setRoomWeight(String sroomweight, String smovename, String sroomname){
ContentValues cv = new ContentValues();
cv.put(KEY_ROOMWEIGHT, sroomweight);
ourDatabase.update(DATABASE_TABLE2, cv, KEY_ROOMMOVEHOLDER + "='" + smovename + "'" + " AND " + KEY_ROOM + "='" + sroomname + "'", null);
}
public long addRooms(String sroommoveholder, String sroom){
ContentValues cv = new ContentValues();
cv.put(KEY_ROOMMOVEHOLDER, sroommoveholder);
cv.put(KEY_ROOM, sroom);
cv.put(KEY_ROOMWEIGHT, "0");
return ourDatabase.insert(DATABASE_TABLE2, null, cv);
}
public long addNewItems(String sitemmoveholder, String sroomholder, String sitemname, String sitemvalue, String sitemweight){
ContentValues cv = new ContentValues();
cv.put(KEY_ITEMMOVEHOLDER, sitemmoveholder);
cv.put(KEY_ROOMHOLDER, sroomholder);
cv.put(KEY_ITEMNAME, sitemname);
cv.put(KEY_ITEMVALUE, sitemvalue);
cv.put(KEY_ITEMWEIGHT, sitemweight);
return ourDatabase.insert(DATABASE_TABLE3, null, cv);
}
public void updateItems(String sitemmoveholder, String sroomholder, String sitemname, String sitemvalue, String sitemweight){
ContentValues cv = new ContentValues();
cv.put(KEY_ITEMVALUE, sitemvalue);
cv.put(KEY_ITEMWEIGHT, sitemweight);
ourDatabase.update(DATABASE_TABLE3, cv, KEY_ITEMMOVEHOLDER + "='" + sitemmoveholder + "'" + " AND " +
KEY_ROOMHOLDER + "='" + sroomholder + "'" + " AND " + KEY_ITEMNAME + "='" + sitemname + "'", null);
}
public Cursor getMove(){
String[] columns = new String[]{KEY_ID1, KEY_MOVENAME};
Cursor c = null;
try {
c = ourDatabase.query(DATABASE_TABLE1, columns, null, null, null, null, null);
} catch (Exception e) {
c.close();
}
return c;
}
public String getTotalWeight(String m) throws SQLException{
String[] columns = new String[]{KEY_ID1, KEY_MOVENAME, KEY_MOVEDATE, KEY_TOTALMOVEWEIGHT};
Cursor c = null;
try {
c = ourDatabase.query(DATABASE_TABLE1, columns, KEY_MOVENAME + "= '" + m + "'", null, null, null, null);
if (c != null) {
c.moveToFirst();
String totalWeight = c.getString(3);
return totalWeight;
}
} catch (Exception e) {
c.close();
}
return null;
}
public String getTotalWeightLBS(String m) throws SQLException{
String[] columns = new String[]{KEY_ID1, KEY_MOVENAME, KEY_MOVEDATE, KEY_TOTALMOVEWEIGHT};
Cursor c = null;
try {
c = ourDatabase.query(DATABASE_TABLE1, columns, KEY_MOVENAME + "= '" + m + "'", null, null, null, null);
if (c != null) {
c.moveToFirst();
int x = Integer.parseInt(c.getString(3)) * 7;
String totalWeight = "" + x + " lbs";
return totalWeight;
}
} catch (Exception e) {
c.close();
}
return null;
}
public String getDateMove(String md){
String[] columns = new String[]{KEY_ID1, KEY_MOVENAME, KEY_MOVEDATE};
Cursor c = null;try {
c = ourDatabase.query(DATABASE_TABLE1, columns, KEY_MOVENAME + "= '" + md + "'", null, null, null, null);
if (c != null) {
c.moveToFirst();
String moveDate = c.getString(2);
return moveDate;
}
} catch (Exception e) {
c.close();
}
return null;
}
public ArrayList<String> loadRooms(String mn) throws SQLException{
String[] columns = new String[]{KEY_ID2, KEY_ROOMMOVEHOLDER, KEY_ROOM};
ArrayList<String> array = new ArrayList<String>();
Cursor c = null;
try {
c = ourDatabase.query(DATABASE_TABLE2, columns,KEY_ROOMMOVEHOLDER + "='" + mn + "'",
null, null, null, null);
int iroom = c.getColumnIndex(KEY_ROOM);
for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
array.add(c.getString(iroom));
}
} catch (Exception e) {
c.close();
}
return array;
}
public void deleteMove(String m) throws SQLException{
ourDatabase.delete(DATABASE_TABLE1, KEY_MOVENAME + "='" + m + "'", null);
ourDatabase.delete(DATABASE_TABLE2, KEY_ROOMMOVEHOLDER + "='" + m + "'", null);
ourDatabase.delete(DATABASE_TABLE3, KEY_ITEMMOVEHOLDER + "='" + m + "'", null);
ourDatabase.delete(DATABASE_TABLE4, KEY_TODOMOVE + "='" + m + "'", null);
}
public ArrayList<String> getitems(){
String[] columns = new String[]{KEY_ID5, KEY_ITEMNAME};
ArrayList<String> items;
items = new ArrayList<String>();
Cursor c = null;
try {
c = ourDatabase.query(DATABASE_TABLE5, columns, null, null, null, null, null);
for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
items.add(c.getString(1));
}
} catch (Exception e) {
c.close();
}
return items;
}
public ArrayList<String> getitemweight(){
String[] columns = new String[]{KEY_ID5, KEY_ITEMWEIGHT};
ArrayList<String> items = new ArrayList<String>();
Cursor c = null;
try {
c = ourDatabase.query(DATABASE_TABLE5, columns, null, null, null, null, null);
for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
items.add(c.getString(1));
}
} catch (Exception e) {
c.close();
}
return items;
}
public ArrayList<String> getitemclass(){
String[] columns = new String[]{KEY_ID5, KEY_LISTITEMROOM};
ArrayList<String> items = new ArrayList<String>();
Cursor c = null;
try {
c = ourDatabase.query(DATABASE_TABLE5, columns, null, null, null, null, null);
for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
items.add(c.getString(1));
}
} catch (Exception e) {
c.close();
}
return items;
}
}
最佳答案
您没有在 getDateMove
、getTotalWeightLBS
、loadRooms
中关闭光标,...
不再需要光标的任何地方都将其关闭。在那些使用try-finally的方法中,即使抛出异常也能保证执行finally block 中的代码。
从这里更改您的方法中的代码:
try{
// get data from cursor
} catch (Exception e) {
c.close();
}
为此:
try {
// get data from cursor
} catch (Exception e) {
// exception handling
} finally {
if(c != null){
c.close();
}
}
关于android - 如何在android中正确关闭光标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12950725/
我正在使用的网站上有一个非 Canvas 导航。关闭 Canvas 导航的默认状态是关闭的,这在移动网站上运行良好,因为您可以打开它并选择您的链接,但在桌面上关闭它并打开它会隐藏用户的信息,我希望它是
我有一个 NSViewController 是这样连接的: 在底部 viewController 中,我尝试使用 self.dismiss(self) 关闭它,但是,它会产生此错误: [General
我昨天制作了一个扩展的 JQuery 搜索框,它的作用就像一个魅力!但是,我在创建一个脚本时遇到问题,当用户单击搜索框时,它会关闭。 这是我的 JQuery: function expandSearc
我一辈子都无法在 API V3 中一次只显示一个信息窗口。我需要一个在下一次开放之前关闭。还希望在 map 上的任何地方关闭 infoWindow onclick。这是否在初始化函数中? 这是我的完整
关闭和清理套接字的正确方法是什么? 我在辅助线程中运行 io_service,我需要关闭与主线程的连接: void closeConnection() { ioc.post([&socket]
我的 Selenium 测试看起来像这样:客户选择金融产品,填写一些必要的数据,并在打印预览中显示条款/协议(protocol)文档(根据本地法律的要求)。打印/关闭打印预览对话框后,客户输入更多数据
我目前正在从 android 网站了解 Navigation Drawer,我正在使用他们的示例 http://developer.android.com/training/implementing-
尝试通过 expo 在模拟器上运行 react-native 应用程序时出现此错误。 Couldn't start project on Android: Error running adb: adb
方法一 function transform(ar) { var alStr = []; for(var i=0; i
我想按以下方式自定义我的抽屉导航: 我希望在抽屉打开时显示一个图标,在抽屉关闭时显示另一个图标,而不是将菜单图标稍微向左滑动的当前默认动画。 关于我在哪里可以找到类似内容的任何想法/线索? 我做了一些
我们刚刚从 0.6.2 或 0.7 升级了我们的 dropwizard 版本,发现 .yml 文件中的很多配置都发生了变化。尽管我们能够弄清楚其中的大部分,但我们无法弄清楚如何关闭“requestLo
从 celery 2.4.5 升级后,我开始让 celery 随机关闭。 我在 centOS 机器上使用 celery 3.0.12、boto 2.6 和 amazon sqs 和 django 1.
我试图包含一些语句来指导用户更多地了解文件无法打开或关闭的原因。文件在写入模式下无法打开的一些可能情况是什么?无法关闭怎么办? FILE *fp; if(!(fp = fopen("testing",
我有一个DLL,可以访问数据库并从存储在配置文件中的应用程序设置中读取连接字符串。然后,引用此DLL的应用程序将需要在其配置文件中为此配置设置设置值。 我遇到的问题是,生成的配置代码会通过Defaul
我将 UIDatePicker 添加为 UITextField 的输入 View UIDatePicker *oBirth; NSDateFormatter *dateFormat; _edit
我有以下代码: SecondViewController *secondView = [[SecondViewController alloc] initWithNibName:@"SecondVie
Closed. This question does not meet Stack Overflow guidelines。它当前不接受答案。 想改善这个问题吗?更新问题,以便将其作为on-topic
通常,按下 option 键关闭窗口会关闭应用程序中的所有窗口。在我的应用程序中,我希望它仅关闭与用户正在关闭的窗口相关的窗口。我怎样才能做到这一点?我可以为所有窗口实现 windowShouldCl
我有一个 NSWindow,它托管一个已连接到脚本处理程序的 WebView。 现在,当用户单击 WebView 上的控件上的按钮时,它会调用我的对象上的 Objective C 方法。 在这种特定情
我想根据 MBP 上的相机使用情况自动化个人工作流程。 基本上我想知道是否任何 的摄像头(内置或 USB)已打开或关闭,因此我可以运行我将创建的程序或脚本。 我认为如果我需要轮询相机状态也可以,但基于
我是一名优秀的程序员,十分优秀!