在我的程序中执行联系人选择的代码时,游标检索值的行导致异常。我无法弄清楚异常发生的原因。任何人都可以帮助我。我的代码如下。
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
谢谢!
答案 0 :(得分:0)
添加以下代码行:
<uses-permission android:name="android.permission.READ_CONTACTS"/>
到结束清单标记之前。
删除了提及此权限的任何其他代码,停止收到此错误。