It walks like a bug, it chirps like a bug.... I need someone to confirm that it's a bug.
I was trying to get phone numbers from the address book with this code:
public JSONObject getAllPhones() throws JSONException{
String mPhoneNumberProjection[] = new String[] {
Contacts.Phones.NAME, Contacts.Phones.NUMBER, Contacts.Phones.TYPE
};
Uri phoneNumbersUri = Contacts.Phones.CONTENT_URI;
JSONObject result = new JSONObject();
JSONArray phones = new JSONArray();
Cursor myCursor = mApp.managedQuery(phoneNumbersUri, mPhoneNumberProjection, null, null, null);
mApp.startManagingCursor(myCursor);
if (!myCursor.isAfterLast()){
myCursor.moveToFirst();
do{
JSONObject aRow = new JSONObject();
for (int i = 0; i < myCursor.getColumnCount(); i++){
Log.d(LOG_TAG, myCursor.getColumnName(i) + " -> " + myCursor.getString(i));
aRow.put(myCursor.getColumnName(i), myCursor.getString(i));
}
phones.put(aRow);
} while (myCursor.moveToNext());
}
result.put("phones", phones);
result.put("phoneTypes", getPhoneTypes());
return result;
}
But when there are no contacts, and !myCursor.isAfterLast() evaluates as "false", program steps into "for" loop. So it enters "if" block, skips several methods and lands in "for" loop...
When I extract this loop into a separate function, everything works ok.
I'm doing this in Eclipse Helios on 32-bit Vista. I tried cleaning the project, erasing Java cache, restarting computer, creating new AVD... I used Android 1.6 and Android 2.2, but the error stays on...
Can someone explain what's going on?