我正在使用gridview在我的应用程序中显示3行书籍封面图像。这些书是从数据库中检索出来的,当一本书被选中时,它会在另一个活动中打开。如果你把手指放在没有书的屏幕上,那么我的应用程序崩溃了,我得到了一个CursorIndexOutOfBoundsException,如下面我的log cat所示。这是我点击gridview时的代码:
@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());
}
我尝试在代码的开头添加一些检查,例如if(cursor.getCount&gt; = 0),但现在仍然很高兴。有谁知道这导致了什么?
08-17 11:36:53.724: E/AndroidRuntime(4883): FATAL EXCEPTION: main
08-17 11:36:53.724: E/AndroidRuntime(4883): android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 71
08-17 11:36:53.724: E/AndroidRuntime(4883): at android.database.AbstractCursor.checkPosition(AbstractCursor.java:400)
08-17 11:36:53.724: E/AndroidRuntime(4883): at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136)
08-17 11:36:53.724: E/AndroidRuntime(4883): at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:50)
08-17 11:36:53.724: E/AndroidRuntime(4883): at com.hmh.android.readers.MyReadersActivity$1.run(MyReadersActivity.java:940)
08-17 11:36:53.724: E/AndroidRuntime(4883): at android.os.Handler.handleCallback(Handler.java:605)
08-17 11:36:53.724: E/AndroidRuntime(4883): at android.os.Handler.dispatchMessage(Handler.java:92)
08-17 11:36:53.724: E/AndroidRuntime(4883): at android.os.Looper.loop(Looper.java:137)
08-17 11:36:53.724: E/AndroidRuntime(4883): at android.app.ActivityThread.main(ActivityThread.java:4533)
08-17 11:36:53.724: E/AndroidRuntime(4883): at java.lang.reflect.Method.invokeNative(Native Method)
08-17 11:36:53.724: E/AndroidRuntime(4883): at java.lang.reflect.Method.invoke(Method.java:511)
08-17 11:36:53.724: E/AndroidRuntime(4883): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
08-17 11:36:53.724: E/AndroidRuntime(4883): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
08-17 11:36:53.724: E/AndroidRuntime(4883): at dalvik.system.NativeStart.main(Native Method)
编辑:我认为这个问题是由我的长按引起的。当您按住屏幕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);
}
}
};
答案 0 :(得分:0)
您正试图将光标移动到无效位置:-1
。忽略无效的职位,您的应用程序不会崩溃。
Cursor cursor = mGridAdapter.getCursor();
if (position < 0 || cursor.getCount() <= position) {
return;
}
cursor.moveToPosition(position);
...