android 获取联系人的姓名和电话号码(适用于 android 1.5)
到目前为止,我从我在这里看到的示例中得到了这段代码:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好的,谢谢 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!