0

在我的程序中执行联系人拾取代码时,光标正在检索值的行导致异常。我无法弄清楚为什么会发生异常。任何人都可以帮助我。我的代码如下。

public void onActivityResult(int requestcode, int resultcode, Intent data){
    super.onActivityResult(requestcode, resultcode, data);

    switch(requestcode){
        case (CONTACT_PICKER_RESULT):
                if(resultcode == Activity.RESULT_OK)
                {
                    Cursor cursor = null;
                    String phno ="";
                    try
                    {
                        Uri my_uri = data.getData();
                        String id = my_uri.getLastPathSegment();
                        Toast.makeText(getApplicationContext(), "id="+id, Toast.LENGTH_SHORT).show();

                        int phnoidx = 0;

                              //Exception occured in the next line
                        cursor = getContentResolver().query(Phone.CONTENT_URI, null, Phone.CONTACT_ID+"=?", new String[] { id }, null);//--> Exception occurs here
                        phnoidx = cursor.getColumnIndex(Phone.DATA);
                        if(cursor.moveToFirst())
                        {
                            while(cursor.isAfterLast() == false)
                            {
                                phno = cursor.getString(phnoidx);
                                Toast.makeText(getApplicationContext(), "no="+phno, Toast.LENGTH_SHORT).show();
                                cursor.moveToNext();
                            }
                        }
                    }
                    catch(Exception e)
                    {
                        Toast.makeText(getApplicationContext(), "Error occured.", Toast.LENGTH_SHORT).show();
                        //exception handle
                    }
                    finally{
                        if(cursor != null)
                            cursor.close();
                    }
                    if(phoneno.getText().equals("") || !addphone_enable)
                        phoneno.setText(phno);
                    else if(addphone_enable)
                        addphno.setText(phno);
                }
                break;
    }
    activitytype = "main";

}

这是我得到的例外:

02-15 12:44:55.594: E/DatabaseUtils(187): java.lang.SecurityException: Permission Denial: reading com.android.providers.contacts.ContactsProvider2 uri content://com.android.contacts/data/phones from pid=336, uid=10036 requires android.permission.READ_CONTACTS

谢谢你!

4

1 回答 1

0

添加以下代码行:

<uses-permission android:name="android.permission.READ_CONTACTS"/>

到结束清单标记之前。

删除了任何其他提到此权限的代码,停止收到此错误。

于 2013-02-15T07:29:22.830 回答