You need to move the Cursor
to a valid row before trying to access column values with get...()
.
Add
if (cursor.moveToFirst()) {
// cursor.get...()
}
around the cursor access.
In case you want all result rows, add a loop, too:
if (cursor.moveToFirst()) {
do {
// cursor.get...()
} while (cursor.moveToNext());
}
This specific stacktrace is from getAllContacts()
but getContact()
has similar problem – you have moveToFirst()
but fail to check if it succeeds.
Additionally getContactsCount()
is broken, too. You shouldn’t access the count of a closed cursor.
solved Unable to save and retrieve String data using Sqlite in android