0

当用户在联系人列表中时,他/她应该从联系人列表中选择一个人,联系人活动将关闭并调用onActivityResult();但是当我点击联系人页面上角的后退按钮或取消按钮时,光标将为空,并且出现空指针异常。和想法?

代码:

selectContactButton.setOnClickListener(new OnClickListener()
        {

            public void onClick(View v)
            {
                Intent intent = new Intent(Intent.ACTION_PICK);
                 intent.setType(ContactsContract.Contacts.CONTENT_TYPE);
                 startActivityForResult(intent, PICK_CONTACT);
            }
        });


public void onActivityResult(int requestCode, int resultCode, Intent intent) 
    {

      if (requestCode == PICK_CONTACT)   //I tried to do if intent!=null then run this codes but there is the same error
      {         

          Cursor cursor =  managedQuery(intent.getData(), null, null, null, null);
          cursor.moveToNext();

           String  name = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME)); 

          editName.setText(name); //edittext view

      }

    }
4

1 回答 1

0

在游标上调用 moveToFirst,并测试它返回的值。

于 2012-08-13T02:47:27.867 回答