我知道你可能会认为这个主题已经被处理了好几次,但这次是不同的!
我的应用程序应该从选定的联系人那里获取联系信息(姓名、号码),但我只得到姓名而无法得到号码。
@Override
public void onClick(View v) {
// Opening Contacts Window as a Window
Intent contactPickerIntent = new Intent(Intent.ACTION_PICK,
ContactsContract.Contacts.CONTENT_URI);
// calling OnActivityResult with intent And Some contact for Identifie
startActivityForResult(contactPickerIntent, PICK);
}
@Override
public void onActivityResult(int reqCode, int resultCode, Intent data) {
super.onActivityResult(reqCode, resultCode, data);
switch (reqCode) {
case (PICK) :
if (resultCode == Activity.RESULT_OK) {
Uri contactData = data.getData();
Cursor c = managedQuery(contactData, null, null, null, null);
if (c.moveToFirst()) {
int indexName = c.getColumnIndex(
ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
int indexNumber = c.getColumnIndex(
ContactsContract.CommonDataKinds.Phone.NUMBER);
nom = c.getString(indexName);
numero = c.getString(indexNumber);
//Visual confirm
Toast.makeText(this, "Contact " + nom +" enregistré!",
Toast.LENGTH_LONG).show();
//Save in prefs:
SharedPreferences manager =
PreferenceManager.getDefaultSharedPreferences(this);
Editor editor = manager.edit();
editor.putString("num", numero);
editor.putString("nom", nom);
editor.commit();
名称是正确的,但数字会导致强制关闭。
但是如果我用下面的替换它不再强制关闭,但数字仍然不正确(0或1)。
int indexNumber = c.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)+1;
我应该怎么办?