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