Android 从 sqlite 读取值时出现 Stackoverflow 错误
Android Stackoverflow error while reading values from sqlite
我正在使用指纹 reader 读取员工指纹。
一旦morphoDatabase.identify
被执行,指纹reader等待员工指纹。
捕获指纹后,我从指纹 reader 数据库中获取 employeeId。
现在我想从我的 sqlite 数据库中获取这个员工的详细信息并显示问候语。
在此之后我重新开始识别过程。
问题:
当我的识别计数达到大约 100 时,我的应用程序崩溃了。
我添加了logcat错误。
Thread commandThread = (new Thread(new Runnable() {
@Override
public void run() {
try {
identifyThreadMethod();
Log.e("Identification Thread", "Completed");
return;
} catch (Exception e) {
e.printStackTrace();
}
}
}));
private void identifyThreadMethod() {
int ret = 0;
try {
ret = morphoDatabase.identify(timeout, far, coder,
detectModeChoice, matchingStrategy, callbackCmd,
DashboardActivity.this, resultMatching, 2, morphoUser);
} catch (Exception e) {
Log.e("Exception", "Identification");
}
if(ret == 0) {
String employeeId= morphoUser.getField(0);
getEmployee(employeeId);
}
if (retvalue != -11) { // reader database not empty
if (!flag) {
identifyThreadMethod();
}
}
}
}
}
public Employee getEmployee(String id) {
if(db == null) {
db = this.getReadableDatabase();
}
String selectQuery = "SELECT * FROM " + TABLE_EMPLOYEE + " WHERE "
+ KEY_ID + "='" + id + "' AND " + KEY_IS_DELETED + "= 0";
//getting Whosebug error on bellow mentioned line
Cursor cursor = db.rawQuery(selectQuery, null);
if (cursor != null)
cursor.moveToFirst();
if (cursor.getCount() > 0) {
Employee employee = new Employee();
employee.setId(cursor.getString(1));
HashMap<Integer, String> templates = new HashMap<>();
templates.put(1, cursor.getString(2));
templates.put(2, cursor.getString(3));
templates.put(3, cursor.getString(4));
templates.put(4, cursor.getString(5));
templates.put(5, cursor.getString(6));
templates.put(6, cursor.getString(7));
templates.put(7, cursor.getString(8));
templates.put(8, cursor.getString(9));
templates.put(9, cursor.getString(10));
templates.put(10, cursor.getString(11));
employee.setTemplateMap(templates);
employee.setFirstName(cursor.getString(12));
employee.setLastName(cursor.getString(13));
employee.setEmpLevel(cursor.getString(14));
employee.setEmpIndex(cursor.getInt(15));
employee.setEmpPIN(cursor.getString(16));
employee.setEmpAccessMode(cursor.getInt(17));
employee.setEmpEnrollFlag(cursor.getInt(18));
employee.setAuthMode(cursor.getInt(19));
employee.setIsDeleted(cursor.getInt(20));
return employee;
}
return null;
}
错误日志
E/AndroidRuntime: FATAL EXCEPTION: pool-1-thread-1
java.lang.WhosebugError
at java.util.WeakHashMap.poll(WeakHashMap.java:569)
at java.util.WeakHashMap.put(WeakHashMap.java:608)
at android.database.sqlite.SQLiteConnectionPool.finishAcquireConnectionLocked(SQLiteConnectionPool.java:911)
at android.database.sqlite.SQLiteConnectionPool.tryAcquirePrimaryConnectionLocked(SQLiteConnectionPool.java:847)
at android.database.sqlite.SQLiteConnectionPool.waitForConnection(SQLiteConnectionPool.java:613)
at android.database.sqlite.SQLiteConnectionPool.acquireConnection(SQLiteConnectionPool.java:348)
at android.database.sqlite.SQLiteSession.acquireConnection(SQLiteSession.java:894)
at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:586)
at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)
at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44)
at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1314)
at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1253)
at com.cms.attendance.fingerprintreader.database.DatabaseHandler.getEmployee(DatabaseHandler.java:434)
at com.cms.attendance.fingerprintreader.newui.DashboardActivity.identifyThreadMethod(DashboardActivity.java:876)
at com.cms.attendance.fingerprintreader.newui.DashboardActivity.identifyThreadMethod(DashboardActivity.java:999)
at com.cms.attendance.fingerprintreader.newui.DashboardActivity.identifyThreadMethod(DashboardActivity.java:999)
at com.cms.attendance.fingerprintreader.newui.DashboardActivity.identifyThreadMethod(DashboardActivity.java:999)
at com.cms.attendance.fingerprintreader.newui.DashboardActivity.identifyThreadMethod(DashboardActivity.java:999)
at com.cms.attendance.fingerprintreader.newui.DashboardActivity.identifyThreadMethod(DashboardActivity.java:999)
at com.cms.attendance.fingerprintreader.newui.DashboardActivity.identifyThreadMethod(DashboardActivity.java:999)
at com.cms.attendance.fingerprintreader.newui.DashboardActivity.identifyThreadMethod(DashboardActivity.java:999)
at com.cms.attendance.fingerprintreader.newui.DashboardActivity.identifyThreadMethod(DashboardActivity.java:999)
at com.cms.attendance.fingerprintreader.newui.DashboardActivity.identifyThreadMethod(DashboardActivity.java:999)
at com.cms.attendance.fingerprintreader.newui.DashboardActivity.identifyThreadMethod(DashboardActivity.java:999)
at com.cms.attendance.fingerprintreader.newui.DashboardActivity.identifyThreadMethod(DashboardActivity.java:999)
at com.cms.attendance.fingerprintreader.newui.DashboardActivity.identifyThreadMethod(DashboardActivity.java:999)
at com.cms.attendance.fingerprintreader.newui.DashboardActivity.identifyThreadMethod(DashboardActivity.java:999)
at com.cms.attendance.fingerprintreader.newui.DashboardActivity.identifyThreadMethod(DashboardActivity.java:999)
at com.cms.attendance.fingerprintreader.newui.DashboardActivity.identifyThreadMethod(DashboardActivity.java:999)
at com.cms.attendance.fingerprintreader.newui.DashboardActivity.identifyThreadMethod(DashboardActivity.java:999)
at com.cms.attendance.fingerprintreader.newui.DashboardActivity.identifyThreadMethod(DashboardActivity.java:999)
at com.cms.attendance.fingerprintreader.newui.DashboardActivity.identifyThreadMethod(DashboardActivity.java:999)
at com.cms.attendance.fingerprintreader.newui.DashboardActivity.identifyThreadMethod(DashboardActivity.java:999)
at com.cms.attendance.fingerprintreader.newui.DashboardActivity.identifyThreadMethod(DashboardActivity.java:999)
at com.cms.attendance.fingerprintreader.newui.DashboardActivity.identifyThreadMethod(DashboardActivity.java:999)
at com.cms.attendance.fingerprintreader.newui.DashboardActivity.identifyThreadMethod(DashboardActivity.java:999)
at com.cms.attendance.fingerprintreader.newui.DashboardActivity.identifyThrea
if (retvalue != -11) { // reader database not empty
if (!flag) {
identifyThreadMethod();
}
}
似乎总是满足这个条件,因此identifyThreadMethod();总是被称为递归。
我建议进行以下更改,以基本上满足游标在完成时关闭的要求。
虽然不确定这是不是问题所在。
- P.S。该代码是 in-principle 并且尚未经过测试,因此可能有错误。
:-
public Employee getEmployee(String id) {
Employee employee; //<<<< Move here to allow cursor close before return.
if(db == null) {
db = this.getReadableDatabase();
}
String selectQuery = "SELECT * FROM " + TABLE_EMPLOYEE + " WHERE "
+ KEY_ID + "='" + id + "' AND " + KEY_IS_DELETED + "= 0";
//getting Whosebug error on bellow mentioned line
Cursor cursor = db.rawQuery(selectQuery, null);
if (cursor.moveToFirst()) { //<<<< Null is a useless check, `moveToFirst` makes `getCount` check redundant.
employee = new Employee();
employee.setId(cursor.getString(1));
HashMap<Integer, String> templates = new HashMap<>();
templates.put(1, cursor.getString(2));
templates.put(2, cursor.getString(3));
templates.put(3, cursor.getString(4));
templates.put(4, cursor.getString(5));
templates.put(5, cursor.getString(6));
templates.put(6, cursor.getString(7));
templates.put(7, cursor.getString(8));
templates.put(8, cursor.getString(9));
templates.put(9, cursor.getString(10));
templates.put(10, cursor.getString(11));
employee.setTemplateMap(templates);
employee.setFirstName(cursor.getString(12));
employee.setLastName(cursor.getString(13));
employee.setEmpLevel(cursor.getString(14));
employee.setEmpIndex(cursor.getInt(15));
employee.setEmpPIN(cursor.getString(16));
employee.setEmpAccessMode(cursor.getInt(17));
employee.setEmpEnrollFlag(cursor.getInt(18));
employee.setAuthMode(cursor.getInt(19));
employee.setIsDeleted(cursor.getInt(20));
}
cursor.close; //<<<< should always close cursors, could be issue.
return employee; //<<<< will return null if no rows
}
请阅读以下答案以更清楚地了解 WhosebugError
我需要 运行 我的应用程序 24*7 并且我正在递归调用 identifyThreadMethod()
,所以堆栈大小在增加。
为了解决这个问题,我返回了我需要调用该方法的条件,以便完成该方法的执行。
Thread commandThread = (new Thread(new Runnable() {
@Override
public void run() {
try {
boolean start = true;
while(start) {
start = identifyThreadMethod();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}));
我正在使用指纹 reader 读取员工指纹。
一旦morphoDatabase.identify
被执行,指纹reader等待员工指纹。
捕获指纹后,我从指纹 reader 数据库中获取 employeeId。
现在我想从我的 sqlite 数据库中获取这个员工的详细信息并显示问候语。
在此之后我重新开始识别过程。
问题: 当我的识别计数达到大约 100 时,我的应用程序崩溃了。
我添加了logcat错误。
Thread commandThread = (new Thread(new Runnable() {
@Override
public void run() {
try {
identifyThreadMethod();
Log.e("Identification Thread", "Completed");
return;
} catch (Exception e) {
e.printStackTrace();
}
}
}));
private void identifyThreadMethod() {
int ret = 0;
try {
ret = morphoDatabase.identify(timeout, far, coder,
detectModeChoice, matchingStrategy, callbackCmd,
DashboardActivity.this, resultMatching, 2, morphoUser);
} catch (Exception e) {
Log.e("Exception", "Identification");
}
if(ret == 0) {
String employeeId= morphoUser.getField(0);
getEmployee(employeeId);
}
if (retvalue != -11) { // reader database not empty
if (!flag) {
identifyThreadMethod();
}
}
}
}
}
public Employee getEmployee(String id) {
if(db == null) {
db = this.getReadableDatabase();
}
String selectQuery = "SELECT * FROM " + TABLE_EMPLOYEE + " WHERE "
+ KEY_ID + "='" + id + "' AND " + KEY_IS_DELETED + "= 0";
//getting Whosebug error on bellow mentioned line
Cursor cursor = db.rawQuery(selectQuery, null);
if (cursor != null)
cursor.moveToFirst();
if (cursor.getCount() > 0) {
Employee employee = new Employee();
employee.setId(cursor.getString(1));
HashMap<Integer, String> templates = new HashMap<>();
templates.put(1, cursor.getString(2));
templates.put(2, cursor.getString(3));
templates.put(3, cursor.getString(4));
templates.put(4, cursor.getString(5));
templates.put(5, cursor.getString(6));
templates.put(6, cursor.getString(7));
templates.put(7, cursor.getString(8));
templates.put(8, cursor.getString(9));
templates.put(9, cursor.getString(10));
templates.put(10, cursor.getString(11));
employee.setTemplateMap(templates);
employee.setFirstName(cursor.getString(12));
employee.setLastName(cursor.getString(13));
employee.setEmpLevel(cursor.getString(14));
employee.setEmpIndex(cursor.getInt(15));
employee.setEmpPIN(cursor.getString(16));
employee.setEmpAccessMode(cursor.getInt(17));
employee.setEmpEnrollFlag(cursor.getInt(18));
employee.setAuthMode(cursor.getInt(19));
employee.setIsDeleted(cursor.getInt(20));
return employee;
}
return null;
}
错误日志
E/AndroidRuntime: FATAL EXCEPTION: pool-1-thread-1
java.lang.WhosebugError
at java.util.WeakHashMap.poll(WeakHashMap.java:569)
at java.util.WeakHashMap.put(WeakHashMap.java:608)
at android.database.sqlite.SQLiteConnectionPool.finishAcquireConnectionLocked(SQLiteConnectionPool.java:911)
at android.database.sqlite.SQLiteConnectionPool.tryAcquirePrimaryConnectionLocked(SQLiteConnectionPool.java:847)
at android.database.sqlite.SQLiteConnectionPool.waitForConnection(SQLiteConnectionPool.java:613)
at android.database.sqlite.SQLiteConnectionPool.acquireConnection(SQLiteConnectionPool.java:348)
at android.database.sqlite.SQLiteSession.acquireConnection(SQLiteSession.java:894)
at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:586)
at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)
at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44)
at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1314)
at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1253)
at com.cms.attendance.fingerprintreader.database.DatabaseHandler.getEmployee(DatabaseHandler.java:434)
at com.cms.attendance.fingerprintreader.newui.DashboardActivity.identifyThreadMethod(DashboardActivity.java:876)
at com.cms.attendance.fingerprintreader.newui.DashboardActivity.identifyThreadMethod(DashboardActivity.java:999)
at com.cms.attendance.fingerprintreader.newui.DashboardActivity.identifyThreadMethod(DashboardActivity.java:999)
at com.cms.attendance.fingerprintreader.newui.DashboardActivity.identifyThreadMethod(DashboardActivity.java:999)
at com.cms.attendance.fingerprintreader.newui.DashboardActivity.identifyThreadMethod(DashboardActivity.java:999)
at com.cms.attendance.fingerprintreader.newui.DashboardActivity.identifyThreadMethod(DashboardActivity.java:999)
at com.cms.attendance.fingerprintreader.newui.DashboardActivity.identifyThreadMethod(DashboardActivity.java:999)
at com.cms.attendance.fingerprintreader.newui.DashboardActivity.identifyThreadMethod(DashboardActivity.java:999)
at com.cms.attendance.fingerprintreader.newui.DashboardActivity.identifyThreadMethod(DashboardActivity.java:999)
at com.cms.attendance.fingerprintreader.newui.DashboardActivity.identifyThreadMethod(DashboardActivity.java:999)
at com.cms.attendance.fingerprintreader.newui.DashboardActivity.identifyThreadMethod(DashboardActivity.java:999)
at com.cms.attendance.fingerprintreader.newui.DashboardActivity.identifyThreadMethod(DashboardActivity.java:999)
at com.cms.attendance.fingerprintreader.newui.DashboardActivity.identifyThreadMethod(DashboardActivity.java:999)
at com.cms.attendance.fingerprintreader.newui.DashboardActivity.identifyThreadMethod(DashboardActivity.java:999)
at com.cms.attendance.fingerprintreader.newui.DashboardActivity.identifyThreadMethod(DashboardActivity.java:999)
at com.cms.attendance.fingerprintreader.newui.DashboardActivity.identifyThreadMethod(DashboardActivity.java:999)
at com.cms.attendance.fingerprintreader.newui.DashboardActivity.identifyThreadMethod(DashboardActivity.java:999)
at com.cms.attendance.fingerprintreader.newui.DashboardActivity.identifyThreadMethod(DashboardActivity.java:999)
at com.cms.attendance.fingerprintreader.newui.DashboardActivity.identifyThreadMethod(DashboardActivity.java:999)
at com.cms.attendance.fingerprintreader.newui.DashboardActivity.identifyThreadMethod(DashboardActivity.java:999)
at com.cms.attendance.fingerprintreader.newui.DashboardActivity.identifyThreadMethod(DashboardActivity.java:999)
at com.cms.attendance.fingerprintreader.newui.DashboardActivity.identifyThreadMethod(DashboardActivity.java:999)
at com.cms.attendance.fingerprintreader.newui.DashboardActivity.identifyThreadMethod(DashboardActivity.java:999)
at com.cms.attendance.fingerprintreader.newui.DashboardActivity.identifyThrea
if (retvalue != -11) { // reader database not empty
if (!flag) {
identifyThreadMethod();
}
}
似乎总是满足这个条件,因此identifyThreadMethod();总是被称为递归。
我建议进行以下更改,以基本上满足游标在完成时关闭的要求。
虽然不确定这是不是问题所在。
- P.S。该代码是 in-principle 并且尚未经过测试,因此可能有错误。
:-
public Employee getEmployee(String id) {
Employee employee; //<<<< Move here to allow cursor close before return.
if(db == null) {
db = this.getReadableDatabase();
}
String selectQuery = "SELECT * FROM " + TABLE_EMPLOYEE + " WHERE "
+ KEY_ID + "='" + id + "' AND " + KEY_IS_DELETED + "= 0";
//getting Whosebug error on bellow mentioned line
Cursor cursor = db.rawQuery(selectQuery, null);
if (cursor.moveToFirst()) { //<<<< Null is a useless check, `moveToFirst` makes `getCount` check redundant.
employee = new Employee();
employee.setId(cursor.getString(1));
HashMap<Integer, String> templates = new HashMap<>();
templates.put(1, cursor.getString(2));
templates.put(2, cursor.getString(3));
templates.put(3, cursor.getString(4));
templates.put(4, cursor.getString(5));
templates.put(5, cursor.getString(6));
templates.put(6, cursor.getString(7));
templates.put(7, cursor.getString(8));
templates.put(8, cursor.getString(9));
templates.put(9, cursor.getString(10));
templates.put(10, cursor.getString(11));
employee.setTemplateMap(templates);
employee.setFirstName(cursor.getString(12));
employee.setLastName(cursor.getString(13));
employee.setEmpLevel(cursor.getString(14));
employee.setEmpIndex(cursor.getInt(15));
employee.setEmpPIN(cursor.getString(16));
employee.setEmpAccessMode(cursor.getInt(17));
employee.setEmpEnrollFlag(cursor.getInt(18));
employee.setAuthMode(cursor.getInt(19));
employee.setIsDeleted(cursor.getInt(20));
}
cursor.close; //<<<< should always close cursors, could be issue.
return employee; //<<<< will return null if no rows
}
请阅读以下答案以更清楚地了解 WhosebugError
我需要 运行 我的应用程序 24*7 并且我正在递归调用 identifyThreadMethod()
,所以堆栈大小在增加。
为了解决这个问题,我返回了我需要调用该方法的条件,以便完成该方法的执行。
Thread commandThread = (new Thread(new Runnable() {
@Override
public void run() {
try {
boolean start = true;
while(start) {
start = identifyThreadMethod();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}));