Android 在屏幕上长按后出现CursorIndexOutOfBoundsException
我正在使用gridview在我的应用程序中显示3行书籍封面图像。这些书籍从数据库中检索,当选择一本书时,它将在另一个活动中打开。如果您将手指放在屏幕上没有书的地方,则我的应用程序崩溃,我会收到一个CursorIndexOutOfBoundsException,如下面的日志猫所示。以下是单击gridview时的代码:Android 在屏幕上长按后出现CursorIndexOutOfBoundsException,android,android-sqlite,Android,Android Sqlite,我正在使用gridview在我的应用程序中显示3行书籍封面图像。这些书籍从数据库中检索,当选择一本书时,它将在另一个活动中打开。如果您将手指放在屏幕上没有书的地方,则我的应用程序崩溃,我会收到一个CursorIndexOutOfBoundsException,如下面的日志猫所示。以下是单击gridview时的代码: @Override public void onItemClick(AdapterView<?> parent, View view, int position, lon
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Cursor cursor = mGridAdapter.getCursor();
cursor.moveToPosition(position);
boolean downloaded = cursor.getInt(cursor.getColumnIndex(DatabaseHelper.COLUMN_BOOK_DOWNLOADED)) == 1;
String remoteFilename = cursor.getString(cursor.getColumnIndex(DatabaseHelper.COLUMN_BOOK_REMOTE_FILENAME));
int grade = cursor.getInt(cursor.getColumnIndex(DatabaseHelper.COLUMN_BOOK_GRADE));
if (!downloaded) {
String isbn = cursor.getString(cursor.getColumnIndex(DatabaseHelper.COLUMN_BOOK_ISBN));
String title = cursor.getString(cursor.getColumnIndex(DatabaseHelper.COLUMN_BOOK_TITLE));
ProgressBar currentProgressBar = (ProgressBar) view.findViewById(R.id.progressBarBookDownload);
mProgressBarMap.put(isbn, currentProgressBar);
ImageView imgCover = (ImageView) view.findViewById(R.id.imageViewCover);
mCoverImageMap.put(isbn, imgCover);
ImageView imgDownloadIcon = (ImageView) view.findViewById(R.id.imageViewDownloadIcon);
mDownloadIconMap.put(isbn, imgDownloadIcon);
imgDownloadIcon.setVisibility(View.INVISIBLE);
String url = getBookCdnUrl(remoteFilename, grade);
Bundle extras = new Bundle();
extras.putString(DownloadTask.PARAM_URL, url);
extras.putString(DatabaseHelper.COLUMN_BOOK_ISBN, isbn);
extras.putString(DatabaseHelper.COLUMN_BOOK_TITLE, title);
mTaskHandler = Groundy.create(DownloadTask.class).callback(this).args(extras).queue(this);
mDownloadList.put(isbn, remoteFilename);
}
// book has already been downloaded, woop
else {
Log.d(TAG, "Clicked book is " + mFile.getAbsolutePath() + "/" + remoteFilename);
openBook(remoteFilename, mFile.getAbsolutePath() + "/" + remoteFilename);
}
Log.d(TAG, "Book Selected: " + ((TextView) view.findViewById(R.id.textViewReadingLevel)).getText());
}
编辑:我认为这个问题是由我的长按造成的。当你按住屏幕3秒钟,它就会删除一本书。当它找不到一本书时,它就会崩溃。见下面的代码:
public void run() {
Log.i(TAG, "Long press! Pos:" + mGridTouchPosition);
Cursor cursor = mGridAdapter.getCursor();
cursor.moveToPosition(mGridTouchPosition);
String remoteFilename = cursor.getString(cursor.getColumnIndex(DatabaseHelper.COLUMN_BOOK_REMOTE_FILENAME));
if (fileExistance(remoteFilename)) {
View v;
int pos = 0;
v = mGridView.getChildAt(pos);
while (!v.isPressed()) {
pos++;
v = mGridView.getChildAt(pos);
}
longPressedIsbn = cursor.getString(cursor.getColumnIndex(DatabaseHelper.COLUMN_BOOK_ISBN));
deleteBook(v);
}
}
};
您试图将光标移动到无效位置:
-1
。忽略无效位置,应用程序将不会崩溃
Cursor cursor = mGridAdapter.getCursor();
if (position < 0 || cursor.getCount() <= position) {
return;
}
cursor.moveToPosition(position);
...
Cursor Cursor=mGridAdapter.getCursor();
如果(position<0 | | cursor.getCount(),我尝试了,但应用程序仍然崩溃,日志中出现了相同的错误。您重建(清理)了吗?我将在一分钟后再次查看您的问题。我假设第940行是cursor.moveToPosition(position)
?是的,我都试过了。请看我上面的编辑。这是我的长按。我会在长按中尝试你的代码。我在长按中使用了你上面的代码,效果很好。非常感谢。
Cursor cursor = mGridAdapter.getCursor();
if (position < 0 || cursor.getCount() <= position) {
return;
}
cursor.moveToPosition(position);
...