0

When my database is empty, or it has just been created I am getting this error,

03-10 17:34:40.758: E/AndroidRuntime(1144): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.adressbooktake2/com.example.adressbooktake2.MainActivity}: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0

Here is my code in my main class,

public class MainActivity extends Activity {
    DBAdaptor db;   
    Cursor cursor;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main); 
        db = new DBAdaptor(this).open();        
        cursor = db.getAllRecords();

        DisplayRecord(cursor);

}

which then calls this code in my DBAdaptor class,

    public Cursor getAllRecords() 

    {       
        Cursor gaRecords = db.query(DATABASE_TABLE, new String[] {KEY_ROWID, KEY_NAME,
                        KEY_PHONENUMBER, KEY_EMAIL}, null, null, null, null, null);     


        gaRecords.moveToFirst();

        return gaRecords;

    }

}

I believe the problem is that when the database has just been created, there is nowhere for the moveToFirst() to go, as there is no data. But I am not sure how to get round this as I need a moveToFirst() for when there is a stocked database.

Anyone see a solution? Have a diagnosed the problem correctly?

4

3 回答 3

1

You can check for an empty Cursor like this:

...
cursor = db.getAllRecords();
if(cursor.getCount() > 0)
    DisplayRecords(cursor);
else
    DisplayNoRecordsMessage();

Or since you posted DisplayRecords() in a previous question, you can also use:

...
if (c != null && !cursor.isAfterLast())
{
    nameTxt.setText(c.getString(1));
    phoneTxt.setText(c.getString(2));
    emailTxt.setText(c.getString(3));
}

Also please read about Java naming convention which states that method names should start with a lowercase letter.

于 2013-03-10T17:48:15.583 回答
0

Cursor.moveToFirst() returns false in case of empty cursor.

See the doc: http://developer.android.com/reference/android/database/Cursor.html#moveToFirst()

I'd say the problem is in DisplayRecord().

Are you fetching data from the cursor in DisplayRecord() ? If so, inside of it you should check if the cursor contains some data, calling moveToFirst and checking its result for example.

Something like

private void DisplayRecord(Cursor c){
    if(!c.moveToFirst()){
         return;
    }
    // do stuff
}
于 2013-03-10T17:50:08.497 回答
0

Use if(cursor .moveToFirst()) to check cursor

public class MainActivity extends Activity {
    DBAdaptor db;   
    Cursor cursor;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main); 
        db = new DBAdaptor(this).open();        
        cursor = db.getAllRecords();

        if(cursor.moveToFirst()){
           DisplayRecord(cursor);
            }


}
于 2013-03-10T17:51:40.240 回答