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?