日志报告数据库或光标未关闭。我基本上有一个带有自定义surfaceview的活动,并使用一个处理程序向该活动报告。当我收到消息时,我会显示一个警报对话框并更新数据库。
private Handler handler = new Handler() {
public void handleMessage(Message msg) {
switch(msg.what) {
case 1:
dh.open();
dh.updateEvent("id", "name", "someone");
dh.close();
successAlert.show();
break;
case 2:
failAlert.show();
break;
}
}
};
以前我没有“dh.close()”,这时日志报告的数据库/游标没有关闭。但自从我把它加进去之后,要花很长时间才能完成。一旦我收到信息,系统似乎就挂了。我是做错什么了还是经常要花这么长时间。我也试过用try块和finally来关闭db。
编辑:
public class DatabaseHelper {
private Database dbHelper;
private SQLiteDatabase db;
private Context context;
public DatabaseHelper(Context context) {
this.context = context;
//database = new Database(context);
}
public void open() {
dbHelper = new Database(context);
db = dbHelper.getWritableDatabase();
}
public void close() {
dbHelper.close();
}
public void updateEvent(int id, String name, int other) {
ContentValues cv = new ContentValues();
cv.put("id", id);
cv.put("name", name);
cv.put("other", other);
db.update("stateTable", cv, "id=" + id, null);
}
public boolean checkState(int id) {
db = dbHelper.getReadableDatabase();
Cursor cursor = db.query("stateTable", null, null, null, null, null, null);
cursor.moveToPosition(id - 1);
int i = cursor.getInt(2);
android.util.Log.d("semajhan", ": " + i);
if (i == 1) {
return true;
} else {
return false;
}
}
}
扩展的sqliteopenhelper:
public class Database extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "events.db";
private static final int DATABASE_VERSION = 1;
private static final String TABLE_NAME = "stateTable";
private static final String ID = "id";
private static final String NAME = "name";
private static final String OTHER = "other";
private static final String DATABASE_CREATE = "CREATE TABLE stateTable (id INT, name TEXT, other INT)";
private static final String DATABASE_UPGRADE = "DROP TABLE IF EXISTS table";
public Database(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(DATABASE_CREATE);
// added initial values
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
db.execSQL(DATABASE_UPGRADE);
onCreate(db);
}
}
01-11 13:57:41.239: ERROR/ActivityManager(61): ANR in com.semajhan.soodles (com.semajhan.soodles/.Level1)
01-11 13:57:41.239: ERROR/ActivityManager(61): Reason: keyDispatchingTimedOut
01-11 13:57:41.239: ERROR/ActivityManager(61): Load: 1.64 / 0.56 / 0.26
01-11 13:57:41.239: ERROR/ActivityManager(61): CPU usage from 35716ms to -1ms ago:
01-11 13:57:41.239: ERROR/ActivityManager(61): 44% 862/com.semajhan.soodles: 37% user + 7.2% kernel / faults: 853 minor
01-11 13:57:41.239: ERROR/ActivityManager(61): 29% 61/system_server: 27% user + 1.9% kernel / faults: 142 minor
01-11 13:57:41.239: ERROR/ActivityManager(61): 0.2% 731/com.android.quicksearchbox: 0% user + 0.2% kernel / faults: 30 minor
01-11 13:57:41.239: ERROR/ActivityManager(61): 0.2% 707/com.android.launcher: 0.2% user + 0% kernel / faults: 30 minor
01-11 13:57:41.239: ERROR/ActivityManager(61): 0.2% 801/com.svox.pico: 0.1% user + 0.1% kernel / faults: 363 minor
01-11 13:57:41.239: ERROR/ActivityManager(61): 0% 117/com.android.systemui: 0% user + 0% kernel
01-11 13:57:41.239: ERROR/ActivityManager(61): 0% 41/adbd: 0% user + 0% kernel
01-11 13:57:41.239: ERROR/ActivityManager(61): 99% TOTAL: 86% user + 13% kernel + 0% irq
01-11 13:57:41.239: ERROR/ActivityManager(61): CPU usage from 1969ms to 2620ms later:
01-11 13:57:41.239: ERROR/ActivityManager(61): 54% 61/system_server: 48% user + 6% kernel
01-11 13:57:41.239: ERROR/ActivityManager(61): 40% 69/SurfaceFlinger: 40% user + 0% kernel
01-11 13:57:41.239: ERROR/ActivityManager(61): 10% 92/InputDispatcher: 7.5% user + 3% kernel
01-11 13:57:41.239: ERROR/ActivityManager(61): 1.5% 62/HeapWorker: 1.5% user + 0% kernel
01-11 13:57:41.239: ERROR/ActivityManager(61): 44% 862/com.semajhan.soodles: 32% user + 12% kernel / faults: 2 minor
01-11 13:57:41.239: ERROR/ActivityManager(61): 24% 874/Thread-13: 24% user + 0% kernel
01-11 13:57:41.239: ERROR/ActivityManager(61): 23% 862/studios.soodles: 4.6% user + 18% kernel
01-11 13:57:41.239: ERROR/ActivityManager(61): 1.5% 867/Compiler: 0% user + 1.5% kernel
01-11 13:57:41.239: ERROR/ActivityManager(61): 0.8% 731/com.android.quicksearchbox: 0% user + 0.8% kernel
01-11 13:57:41.239: ERROR/ActivityManager(61): 0.8% 732/HeapWorker: 0% user + 0.8% kernel
01-11 13:57:41.239: ERROR/ActivityManager(61): 100% TOTAL: 76% user + 23% kernel
最佳答案
对getReadableDatabase
实例的getWritableDatabase
或SQLiteOpenHelper
的第一次调用需要很长时间才能完成。您不应该每次需要查询数据库时都创建一个新的Database
对象(您的SQLiteOpenHelper
实例)。尝试在Database
中使用相同的DatabaseHelper
实例。
当使用SQLiteOpenHelper
时,您不想关闭SQLiteDatabase
对象,因为它是共享的;即SQLiteOpenHelper
总是返回相同的getWritableDatabase
对象。
注意,您的SQLiteDatabase
方法泄漏了一个游标。为了帮助防止游标泄漏,我总是在获取游标后使用checkState
-try
。例如:
db = dbHelper.getReadableDatabase();
Cursor cursor = db.query("stateTable", null, null, null, null, null, null);
try {
cursor.moveToPosition(id - 1);
int i = cursor.getInt(2);
android.util.Log.d("semajhan", ": " + i);
if (i == 1) {
return true;
} else {
return false;
}
} finally {
cursor.close();
}
关于java - Android sqlite日志报告和db.close(),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/4660363/