0
07-05 23:23:27.497: ERROR/AndroidRuntime(390): java.lang.RuntimeException: Unable to resume activity {com.fttech.books/com.fttech.books.creatBook}: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0

07-05 23:23:27.497: ERROR/AndroidRuntime(390): Caused by: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0

I keep getting these errors when i run i change views and it calls my onResume method to re-populate EditText fields. Here is my code for on resume:

protected void onResume(){
    Log.v(tag, "In onResume!");
    Cursor it = mDbHelper.fetchBook(mRowId);
    super.onResume();
    mDbHelper.open();
    setRowIdFromIntent();
    if (it.moveToFirst()) {
       populateFields();


    }

Here is my setRowIdFromIntent method:

private void setRowIdFromIntent(){
    if(mRowId == null){
        Bundle extras = getIntent().getExtras();
        mRowId = extras != null 
        ? extras.getLong(DbAdapter.KEY_ROWID)
                :null;
    }

PopulateFields method which retrieves information from the database if the activity is paused and then resumed, such as when i change from porttrait to landscape.

private void populateFields() {
    if(mRowId != null){
        Cursor books = mDbHelper.fetchBook(mRowId);
        startManagingCursor(books);

        bookTitle.setText(books.getString(books.getColumnIndexOrThrow(DbAdapter.KEY_BOOK)));
        bookAuthor.setText(books.getString(books.getColumnIndexOrThrow(DbAdapter.KEY_AUTHOR)));
        bookIsbn.setText(books.getString(books.getColumnIndexOrThrow(DbAdapter.KEY_ISBN)));
        ratingBar.setRating(books.getFloat(books.getColumnIndex(DbAdapter.KEY_RATING)));
yoshi24
  • 3,147
  • 8
  • 45
  • 62

2 Answers2

0

Use the moveToFirst() method of the cursor. Your code should be something like:

if (cursor.moveToFirst()) {
    \\ populate fields
} else {
    \\ not found!
}

Also make sure that your query returns the columns you are trying to fetch.

aromero
  • 25,681
  • 6
  • 57
  • 79
  • The only thing im trying to do is just incase the user has text typed into the edittext box and then they change the view to portrait i dont want them to lose the information they already typed. Would this be the best way of doing this? – yoshi24 Jul 06 '11 at 00:05
  • I update the populate field to what you said it should be now i get a nullPointerException error cause by the Cursor it = line. – yoshi24 Jul 06 '11 at 00:12
  • You got it wrong, you should do that in the populateFields method, using the "books" cursor. – aromero Jul 06 '11 at 00:14
  • Also if you are interested in saving the state of the activity, refer to this SO question http://stackoverflow.com/questions/151777/how-do-i-save-an-android-applications-state – aromero Jul 06 '11 at 00:21
  • Awesome! it worked, But from my code i think it may be better to store my info in a bundle and restore it vs doing the populate in onResume? it seems less expensive. What do you think? – yoshi24 Jul 06 '11 at 00:26
  • That seems less expensive than the way im doing.. it may be better. How would i do it for onPause? @OVerride onpause(Bundle savedInstanceState) doesnt work. – yoshi24 Jul 06 '11 at 00:42
-1

I have written a ORM framework for that. https://github.com/ahmetalpbalkan/orman

You can easily write Android applications using SQLite with that. It uses your Java classes (you can create Book class in this case) as database tables (entities).

If you use this framework, you won't see cursors at all. All you'll see is going to be:

List<Book> books = Model.fetchAll(Book.class);

then you'll have a list of all books in the database. You won't see column mappings, database and table generation at all. Very useful in your case.

ahmet alp balkan
  • 42,679
  • 38
  • 138
  • 214