reading contacts from contactlist in android
<p>i am developing a sms app in which i want to read contact from the contact list . i have taken all necessary permission like read_contacts ,write_contacts in manifest file ...i am posting only neceessary part of code here this code is unable to read contact from the android phone ..it works on some emulator but not on all and it is not working on the device.</p>

<pre><code>addcontact.setOnClickListener(new View.OnClickListener()
  {
    public void onClick(View V)     
       { 
                 IntentContactPickerIntent=newIntent(Intent.ACTION_PICK,ContactsContract.Contacts.CONTENTI);       
ContactPickerIntent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE); 
startActivityForResult(ContactPickerIntent, CONTACT_PICKER_RESULT);

} } );


protected void onActivityResult(int requestCode, int resultCode, Intent data) {

if (resultCode == RESULT_OK) {
switch (requestCode) {
case CONTACT_PICKER_RESULT: Cursor cursor=null; try {
Uri result = data.getData(); Log.v(DEBUG_TAG, "Got a contact result: " +
result.toString());
</code></pre>

<p>// get the contact id from the Uri<br>
                 String id = result.getLastPathSegment();</p>

<pre><code>             // query for everything contact number  
             cursor = getContentResolver().query(  
                  Phone.CONTENT_URI, null,  
                  Phone.CONTACT_ID + "=?",  
                  new String[]{id}, null); 

             cursor.moveToFirst();
             int phoneIdx = cursor.getColumnIndex(Phone.NUMBER);  
             if (cursor.moveToFirst())
             {   
                 phonenofromcontact = cursor.getString(phoneIdx);
                 finallistofnumberstosendmsg +=","+phonenofromcontact;
                 Log.v(DEBUG_TAG, "Got phone no : " + phonenofromcontact);  
             }
             else 
             {                                
                 Log.w(DEBUG_TAG, "No results"); 
             }
         }
         catch(Exception e)
         {
             Log.e(DEBUG_TAG, "Failed to get contact number", e);
         }
         finally
         {
             if (cursor != null)
             {  
                 cursor.close();
             }
         }
         phonePhoneno= (EditText)findViewById(R.id.phonenofromcontact);
         phonePhoneno.setText(finallistofnumberstosendmsg);
         //phonePhoneno.setText(phonenofromcontact);
         if(phonenofromcontact.length()==0)
         {
             Toast.makeText(this, "No contact number found for this contact",
                     Toast.LENGTH_LONG).show(); 
         }
        break;  
     }  
 } 
 else
 {  
     Log.w(DEBUG_TAG, "Warning: activity result not ok");
 }  
</code></pre>

<p>} </p>
