Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/179.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android cursoroutofboundexception_Android_Cursor_Contactscontract - Fatal编程技术网

Android cursoroutofboundexception

Android cursoroutofboundexception,android,cursor,contactscontract,Android,Cursor,Contactscontract,我正在做一个简单的应用程序,我有一个edittext字段和一个按钮,按钮有一个onclick事件。代码如下所示: private static final int CONTACT_PICKER_RESULT = 1; public void doLaunchContactPicker(View view) { Intent contactPickerIntent = new Intent(Intent.ACTION_PICK, Contacts.CO

我正在做一个简单的应用程序,我有一个edittext字段和一个按钮,按钮有一个onclick事件。代码如下所示:

private static final int CONTACT_PICKER_RESULT = 1;  

public void doLaunchContactPicker(View view) {  
    Intent contactPickerIntent = new Intent(Intent.ACTION_PICK,  
            Contacts.CONTENT_URI);  
    startActivityForResult(contactPickerIntent, CONTACT_PICKER_RESULT);  
}
  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();       
  } 
我正在调用contacts picker活动,当我选择要在edittext字段中填充联系人姓名的联系人时

其代码如下所示:

private static final int CONTACT_PICKER_RESULT = 1;  

public void doLaunchContactPicker(View view) {  
    Intent contactPickerIntent = new Intent(Intent.ACTION_PICK,  
            Contacts.CONTENT_URI);  
    startActivityForResult(contactPickerIntent, CONTACT_PICKER_RESULT);  
}
  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();       
  } 
所有这些代码都包含在主活动的
onCreate()

当我在emulator中运行应用程序并单击按钮时,我将获得联系人列表(我在emulator中创建了3个联系人)。当我选择一个联系人时,我在DDMS中得到一个错误。错误是:

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

您必须首先调用cursor.moveToFirst()。光标的第一个位置始终为-1