Android get contact number using name
<p>I am trying to make an application on android that takes the contact name as a string input and returns his phone number if that contact exists in the phone book...</p>

<p>I tried searching around but there is no clear tutorial as to how to do exactly that</p>

<p>input:contact name
outputs:the phone number</p>

<p>please help </p>

<pre><code>      Cursor cursor = context.getContentResolver().query(ContactsContract.Contacts.CONTENT_URI,null, null, null, null); 


     while (cursor.moveToNext()) { 
         String name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));

         if(name.equalsIgnoreCase(token3)) {

        try{     ContentResolver cr = context.getContentResolver();
             Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[] { ContactsContract.CommonDataKinds.Phone._ID}, null);
             String lname = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));

         Toast.makeText(context, "number is "+lname, Toast.LENGTH_LONG).show();
        }catch (Exception e) {
            // TODO: handle exception
        }

         }
           } 
</code></pre>

<p>it's what I have so far. the piece of code in the try catch block always crashes. </p>
