执行以下代码后,我希望通过TextView resultText显示联系人列表。
预期结果:
$name = $_POST['name'];
$surname = $_POST['surname'];
然而,在执行1 Peter 1 234-567-8
2 Sam 1122-334-4
3 Mary 1 357-246-8
后它崩溃了。错误日志" EXED5"没有显示。我已经在Cursor c = cr.query(contacts, projection, null, null, null);
中添加了权限:
AndroidManifest.xml
代码:
<uses-permission android:name="android.permission.READ_CONTACTS" />
</manifest>
错误日志:
Log.d(TAG, "EXED1");
Uri contacts = Contacts.CONTENT_URI;
Log.d(TAG, "EXED2");
String[] projection = {
ContactsContract.Contacts._ID,
ContactsContract.Contacts.DISPLAY_NAME,
ContactsContract.Contacts.HAS_PHONE_NUMBER
};
Log.d(TAG, "EXED3");
ContentResolver cr = getContentResolver();
Log.d(TAG, "EXED4");
Cursor c = cr.query(contacts, projection, null, null, null);
Log.d(TAG, "EXED5"); //Not displayed in the log but it display "AndroidRuntime: Shutting down VM"
String list = "";
int column;
String idStr, nameStr, phoneStr;
Log.d(TAG, "EXED6");
c.moveToFirst();
do {
Log.d(TAG, "EXED_7_LOOP1");
column = c.getColumnIndex(ContactsContract.Contacts._ID);
idStr = c.getString(column);
column = c.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);
nameStr = c.getString(column);
list += idStr + " " + nameStr;
column = c.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER);
if (c.getString(column).equals("1")) {
Cursor phoneC = cr.query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + idStr,
null,
null
);
phoneC.moveToFirst();
phoneStr = phoneC.getString(phoneC.getColumnIndex(
ContactsContract.CommonDataKinds.Phone.NUMBER
));
list += " " + phoneStr;
}
} while(c.moveToNext());
c.close();
resultText.setText(list);
如何解决问题?
感谢您的关注。