android 获取联系人的姓名和电话号码(适用于 android 1.5)

发布于 2024-09-12 05:43:37 字数 1215 浏览 5 评论 0原文

到目前为止,我从我在这里看到的示例中得到了这段代码:

public class testLayout extends Activity {
final int PICK_CONTACT = 0;
ImageView image = null;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    image=(ImageView)findViewById(R.id.icon);
    image.setOnClickListener(onChangePerson);
}

private View.OnClickListener onChangePerson=new View.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_PICK, People.CONTENT_URI);
startActivityForResult(intent, PICK_CONTACT);

}
};

@Override
 public void onActivityResult(int reqCode, int resultCode, Intent data) {
 super.onActivityResult(reqCode, resultCode, data);

switch (reqCode) {
 case (PICK_CONTACT) :
   if (resultCode == Activity.RESULT_OK) {
     Uri contactData = data.getData();
     Cursor c =  managedQuery(contactData, null, null, null, null);
     if (c.moveToFirst()) 
     {
       String name = c.getString(c.getColumnIndexOrThrow(People.NAME));

     }
   }
   break;
   }
   }
   }

这允许我打开一个正确显示手机上联系人的活动,然后让我选择一个联系人。但是,每次我单击联系人时,程序都会崩溃。有什么想法为什么会发生这种情况吗? 谢谢

So far I have this code from examples I have seen on here:

public class testLayout extends Activity {
final int PICK_CONTACT = 0;
ImageView image = null;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    image=(ImageView)findViewById(R.id.icon);
    image.setOnClickListener(onChangePerson);
}

private View.OnClickListener onChangePerson=new View.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_PICK, People.CONTENT_URI);
startActivityForResult(intent, PICK_CONTACT);

}
};

@Override
 public void onActivityResult(int reqCode, int resultCode, Intent data) {
 super.onActivityResult(reqCode, resultCode, data);

switch (reqCode) {
 case (PICK_CONTACT) :
   if (resultCode == Activity.RESULT_OK) {
     Uri contactData = data.getData();
     Cursor c =  managedQuery(contactData, null, null, null, null);
     if (c.moveToFirst()) 
     {
       String name = c.getString(c.getColumnIndexOrThrow(People.NAME));

     }
   }
   break;
   }
   }
   }

This allows me to open up an activity that correctly shows the contacts on the phone, and let's me choose a contact. However, everytime I click on a contact, the program crashes. Any ideas why this is happening?
Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

音栖息无 2024-09-19 05:43:37

好的,谢谢 Hamy 向我指出 logcat,我以前对此一无所知。

事实证明,我一遍又一遍地看到我在 Android 清单文件中的“需要 READ_CONTACTS 权限”。不幸的是,我似乎将它放在应用程序标签内,这导致程序崩溃而没有告诉我原因。
谢谢你帮我解决这个问题!

Ok so, thank you Hamy for pointing out to me logcat, which I previously had no knowledge of.

It turns out that I was seeing over and over "requires permission READ_CONTACTS" which I had in my android manifest file. Unfortunately, it seems I had it inside of the application tag, which was causing the program to crash without telling me why.
Thank you for helping me solve this!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文