80,337
社区成员
发帖
与我相关
我的任务
分享
public ArrayList<HashMap<String, String>> getDate() {
ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
String name;
String phoneNum;
Cursor cursor = getContentResolver().query(People.CONTENT_URI,
null, null, null, null);
int nameColumn = cursor.getColumnIndex(People.NAME);
int phoneColumn = cursor.getColumnIndex(People.NUMBER);
for(cursor.moveToFirst();!cursor.isAfterLast();cursor.moveToNext()){
name = cursor.getString(nameColumn);
[color=#FF0000]phoneNum = cursor.getString(phoneColumn); [/color]
HashMap<String, String> map = new HashMap<String, String>();
map.put("name",name);
map.put("num", phoneNum);
mylist.add(map);
}
return mylist;
}[code]以上为代码,红色字体段为null,得不到号码,可是我的通讯录上明明有号码啊.为什么得不到呢,报空指针异常
一下为异常[code=Java]11-24 04:03:09.474: ERROR/AndroidRuntime(563): FATAL EXCEPTION: main
11-24 04:03:09.474: ERROR/AndroidRuntime(563): java.lang.NullPointerException
11-24 04:03:09.474: ERROR/AndroidRuntime(563): at com.sirius.android.MyListViewActivity$2.onItemSelected(MyListViewActivity.java:76)
11-24 04:03:09.474: ERROR/AndroidRuntime(563): at android.widget.AdapterView.fireOnSelected(AdapterView.java:864)
11-24 04:03:09.474: ERROR/AndroidRuntime(563): at android.widget.AdapterView.access$200(AdapterView.java:42)
11-24 04:03:09.474: ERROR/AndroidRuntime(563): at android.widget.AdapterView$SelectionNotifier.run(AdapterView.java:830)
11-24 04:03:09.474: ERROR/AndroidRuntime(563): at android.os.Handler.handleCallback(Handler.java:587)
11-24 04:03:09.474: ERROR/AndroidRuntime(563): at android.os.Handler.dispatchMessage(Handler.java:92)
11-24 04:03:09.474: ERROR/AndroidRuntime(563): at android.os.Looper.loop(Looper.java:123)
11-24 04:03:09.474: ERROR/AndroidRuntime(563): at android.app.ActivityThread.main(ActivityThread.java:4627)
11-24 04:03:09.474: ERROR/AndroidRuntime(563): at java.lang.reflect.Method.invokeNative(Native Method)
11-24 04:03:09.474: ERROR/AndroidRuntime(563): at java.lang.reflect.Method.invoke(Method.java:521)
11-24 04:03:09.474: ERROR/AndroidRuntime(563): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
11-24 04:03:09.474: ERROR/AndroidRuntime(563): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
11-24 04:03:09.474: ERROR/AndroidRuntime(563): at dalvik.system.NativeStart.main(Native Method)
1. Cursor cursor = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI,null, null, null, null);
2. while (cursor.moveToNext())
3. {
4. String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
5. String hasPhone = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
6. if (Boolean.parseBoolean(hasPhone))
7. {
8. Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ contactId,null, null);
9. while (phones.moveToNext())
10. {
11. String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
12. }
13. phones.close();
14. }
15. Cursor emails = getContentResolver().query(ContactsContract.CommonDataKinds.Email.CONTENT_URI,null,ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = " + contactId,null, null);
16. while (emails.moveToNext())
17. {
18. String emailAddress = emails.getString(emails.getColumnIndex(ContactsContract.CommonDataKinds.CommonDataColumns.DATA));
19. }
20. emails.close();
21. }
22. cursor.close();
int nameColumn = cursor.getColumnIndex(People.NAME);
int phoneColumn = cursor.getColumnIndex(People.NUMBER);