开发者

Android cursoroutofboundexception

开发者 https://www.devze.com 2023-03-11 08:36 出处:网络
I am doing a simple app, I have an edittext field and a button, the button has an onclick event. The code is as shown below:

I am doing a simple app, I have an edittext field and a button, the button has an onclick event. The code is as shown below:

private static final int CONTACT_PICKER_RESULT = 1;  

public void doLaunchContactPicker(View view) {  
    Intent contactPickerIntent = new Intent(Intent.ACTION_PICK,  
            Contacts.CONTENT_URI);  
    star开发者_Go百科tActivityForResult(contactPickerIntent, CONTACT_PICKER_RESULT);  
}

I am calling contacts picker activity, when I select a contact I want to populate the name of the contact in the edittext field.

The code for this as shown below:

  protected void onActivityResult(int requestCode, int resultCode, Intent data)
  {   
     String contactname = "";  
     // use the contact provider to get the contact details
     Uri result = data.getData();  
     Cursor  cursor = getContentResolver().query(result, null, null, null, null);
     int idx = cursor.getColumnIndexOrThrow(ContactsContract.Contacts._ID);
     contactname = cursor.getString(idx);
     EditText name = (EditText) findViewById(R.id.editText1);  
     name.setText(contactname);
     cursor.close();       
  } 

All this code is included in the onCreate() of the main activity.

When I run the app in emulator and click on the button I am getting contact list (I have created 3 contacts in the emulator). when I select a contact I am getting an error in DDMS. the error is:

06-09 19:24:16.188: ERROR/AndroidRuntime(7158): Caused by: android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 1


You have to call cursor.moveToFirst() first. A cursor's first position is always -1.

0

精彩评论

暂无评论...
验证码 换一张
取 消