【问题标题】:Android: Get contact name using phone numberAndroid:使用电话号码获取联系人姓名
【发布时间】:2017-07-28 07:43:29
【问题描述】:

我正在尝试访问联系人,但不断收到空指针错误。

NullPointerException: Attempt to invoke virtual method ' android.content.ContentResolver android.content.Context.getContentResolver()' on a null object reference

代码

 public static String getContactName(Context context, String phoneNumber) {
    Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phoneNumber));
    Cursor cursor = context.getContentResolver().query(uri, new String[]{ContactsContract.PhoneLookup.DISPLAY_NAME}, null, null, null);
    if (cursor == null) {
        return null;
    }
    String contactName = null;
    String contactNumber = "";
    if(cursor.moveToFirst()) {
        contactName = cursor.getString(cursor.getColumnIndex(ContactsContract.PhoneLookup.DISPLAY_NAME));
    }

    if(cursor != null && !cursor.isClosed()) {
        cursor.close();
    }

    return contactNumber.equals("") ? phoneNumber : contactName;
}

这是我的使用方法

public class VideoActivity extends Activity {

String contactName;
String phoneNumber;
Context context;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_video);

    //Set caller phone
    String number = getIntent().getStringExtra(
            TelephonyManager.EXTRA_INCOMING_NUMBER);

   contactName = getContactName(context, number);
    TextView text = (TextView) findViewById(R.id.textView2);
    text.setText(contactName);

}

是否可以在活动中调用 BroadcastReceiver?

任何帮助将不胜感激。谢谢

【问题讨论】:

  • 您如何称呼您的 getContactName。您的上下文似乎为空,因此出现错误。您可以从您在帖子中调用此方法的位置添加您的方法调用代码
  • 您的上下文为空,您可以从调用方法的位置显示
  • 好的。我会添加它
  • 我更新了调用方法。是否可以在活动中调用 BroadcastReceiver?我猜这就是它返回 null 的原因?
  • @Olalekan 检查我的答案。

标签: java android contact


【解决方案1】:

你没有在你的活动中初始化值context,你是从活动中调用的,所以你可以这样调用,

contactName = getContactName(this, number);

或者你可以设置上下文值并像这样调用,

context = this;
contactName = getContactName(context, number);

【讨论】:

  • 谢谢。让我试试,我会反馈的。
【解决方案2】:
private void ContactList(){
        Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
        String selection = ContactsContract.Contacts.HAS_PHONE_NUMBER;
        Cursor cursor = context.getContentResolver().query(uri, new String[]{ContactsContract.CommonDataKinds.Phone.NUMBER, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME}, selection, null, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " ASC");


        cursor.moveToFirst();
        while (cursor.isAfterLast() == false) {

            String contactNumber = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
            String contactName = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));

            }
            cursor.moveToNext();
        }
        cursor.close();
        cursor = null;

      }

【讨论】:

    猜你喜欢
    • 2015-06-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多