我从SQLite查询字段ID和8字段字符串(有些可以为空)。 我有一个getSearchedItemsFromSQLite方法,只要EditText对象有一个更改状态(自动搜索)就会触发,并且这些错误消息会随机出现,但应用程序根本没有崩溃。
07-27 18:24:52.619: ERROR/AndroidRuntime(1173): ERROR: thread attach failed
07-27 18:24:55.188: ERROR/AndroidRuntime(1184): ERROR: thread attach failed
07-27 18:25:18.649: ERROR/GuitarTabs(1191): getSearchedItemsFromSQLite: Handler for DB + cursor: Index 6 requested, with a size of 6
07-27 18:25:18.659: WARN/System.err(1191): android.database.CursorIndexOutOfBoundsException: Index 6 requested, with a size of 6
07-27 18:25:18.889: WARN/System.err(1191): at android.database.AbstractCursor.checkPosition(AbstractCursor.java:580)
07-27 18:25:18.889: WARN/System.err(1191): at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:214)
07-27 18:25:18.889: WARN/System.err(1191): at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:41)
07-27 18:25:18.889: WARN/System.err(1191): at com.package.project.activity.ItemBrowserActivity.getSearchedItemsFromSQLite(ItemBrowserActivity.java:351)
07-27 18:25:18.889: WARN/System.err(1191): at com.package.project.activity.ItemBrowserActivity$AppInitialization.doInBackground(ItemBrowserActivity.java:199)
07-27 18:25:18.889: WARN/System.err(1191): at com.package.project.activity.ItemBrowserActivity$AppInitialization.doInBackground(ItemBrowserActivity.java:1)
07-27 18:25:18.889: WARN/System.err(1191): at android.os.AsyncTask$2.call(AsyncTask.java:185)
07-27 18:25:18.899: WARN/System.err(1191): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
07-27 18:25:18.909: WARN/System.err(1191): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
07-27 18:25:18.909: WARN/System.err(1191): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068)
07-27 18:25:18.909: WARN/System.err(1191): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561)
07-27 18:25:18.909: WARN/System.err(1191): at java.lang.Thread.run(Thread.java:1096)
我该如何解决这个问题?
以下是该方法的代码段:
dbHelper.open();
cursor = dbHelper.fetchAllTabs(fetchMethod);
startManagingCursor(cursor);
if(cursor != null && cursor.getCount() > 0) {
if(cursor.moveToFirst()) {
while (cursor.isAfterLast() == false) {
if(cursor.getColumnCount() > 0) {
Log.i(logAppName, "/ - Column count is: " + cursor.getColumnCount());
if(!cursor.isNull(0) && !cursor.isNull(1)) {
if(!cursor.isNull(0)) {
ListedItem tab = new ListedItem();
tab.setSongID(cursor.getInt(0));
if(!cursor.isNull(1)) tab.setTitle(cursor.getString(1));
if(!cursor.isNull(2)) tab.setArtist(cursor.getString(2));
if(!cursor.isNull(3)) tab.setDifficulty(cursor.getString(3));
if(!cursor.isNull(4)) tab.setGenre(cursor.getString(4));
if(!cursor.isNull(5)) tab.setFilename(cursor.getString(5));
if(!cursor.isNull(6)) tab.setFileformat(cursor.getString(6));
if(!cursor.isNull(7)) tab.setSource(cursor.getString(7));
if(!cursor.isNull(8)) tab.setDeleted(cursor.getString(8));
if(Integer.parseInt(tab.getDeleted()) == 0)
ret.add(tab);
else
Log.i(logAppName, "Not adding tab: " + tab.getSongTitle() + " (" + tab.getSongFilename() + ") " + "since it's already deleted.");
cursor.moveToNext();
} else {
Log.i(logAppName, "x - ID is null.");
}
} else {
Log.i(logAppName, "x - Either id or title is null.");
}
} else {
Log.i(logAppName, "x - Column count is: " + cursor.getColumnCount());
}
}
}
if(cursor != null)
cursor.close();
答案 0 :(得分:1)
我添加了这些条件:
if ( cursor != null && cursor.getCount() > 0 ) {
totalRows = cursor.getCount();
if(cursor.moveToFirst()) {
while (cursor.isAfterLast() == false) {
if(cursor.getColumnCount() > 0) {
...
之后,我添加了一个sleep线程,允许先前的进程先完成,然后再进行另一个进程:
private boolean processing = false;
...
int ctr = 0;
// added fix to avoid error on cursor
while(processing) {
try {
Log.i(logAppName, "Still processing. Count: " + ctr + ". Is processing? " + processing);
ctr++;
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
if(ctr > 5) {
break;
}
}