如何从 People.CONTENT_URI 返回而不出现错误?
我想获取我的 Android 原始联系人的姓名和号码。但是,当我到达 Android 联系人列表并决定不将任何内容放入我的应用程序然后单击后退按钮时,我收到错误。这是我的代码部分:
public void addContact(){
Intent addContact = new Intent (Intent.ACTION_PICK, People.CONTENT_URI);
startActivityForResult(addContact, ADD_EXIST);
}
@Override
public void onActivityResult(int reqCode, int ResultCode, Intent data){
String wl_name, wl_num;
switch(reqCode){
case (ADD_EXIST):
Uri contactData = data.getData();
Cursor c = managedQuery(contactData, null, null, null, null);
if (c.moveToFirst()) {
wl_name = c.getString(c.getColumnIndexOrThrow(People.NAME));
wl_num = c.getString(c.getColumnIndexOrThrow(People.NUMBER));
createListItem(wl_name, wl_num);
}
break;
default:
(Toast.makeText(getApplicationContext(),
"No Contacts Added", Toast.LENGTH_SHORT)).show();
}
}
如何按下后退按钮而不出现错误。有没有更好的方法来获取联系人?
I want to get the names and numbers of my android original contacts. However, when I reach android contacts list and decide not to get any into my application then click back button, I get an error. Here's the portion of my code:
public void addContact(){
Intent addContact = new Intent (Intent.ACTION_PICK, People.CONTENT_URI);
startActivityForResult(addContact, ADD_EXIST);
}
@Override
public void onActivityResult(int reqCode, int ResultCode, Intent data){
String wl_name, wl_num;
switch(reqCode){
case (ADD_EXIST):
Uri contactData = data.getData();
Cursor c = managedQuery(contactData, null, null, null, null);
if (c.moveToFirst()) {
wl_name = c.getString(c.getColumnIndexOrThrow(People.NAME));
wl_num = c.getString(c.getColumnIndexOrThrow(People.NUMBER));
createListItem(wl_name, wl_num);
}
break;
default:
(Toast.makeText(getApplicationContext(),
"No Contacts Added", Toast.LENGTH_SHORT)).show();
}
}
How can I press back button without getting the error. Is there any better way to get the contacts?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在使用数据之前,您需要检查结果代码,或者至少检查数据是否为空。
You need to check the resultcode or, at the very least, whether data is null before you use it.