A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 陈莹 中级黑马   /  2012-9-18 14:00  /  1062 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

我的程序是获得手机的通讯录,为什么会出现空指向异常?
  1. package imut.chenying;

  2. import java.util.ArrayList;
  3. import java.util.HashMap;
  4. import java.util.List;
  5. import java.util.Map;

  6. import android.app.Activity;
  7. import android.content.Intent;
  8. import android.database.Cursor;
  9. import android.net.Uri;
  10. import android.os.Bundle;
  11. import android.provider.ContactsContract;
  12. import android.view.View;
  13. import android.widget.AdapterView;
  14. import android.widget.AdapterView.OnItemClickListener;
  15. import android.widget.ListView;
  16. import android.widget.SimpleAdapter;

  17. public class AddContract extends Activity {

  18. private String name;
  19. private String number;
  20. ListView lv;

  21. public String getName() {
  22. return name;
  23. }

  24. public void setName(String name) {
  25. this.name = name;
  26. }

  27. public String getNumber() {
  28. return number;
  29. }

  30. public void setNumber(String number) {
  31. this.number = number;
  32. }

  33. @Override
  34. protected void onCreate(Bundle savedInstanceState) {
  35. // TODO Auto-generated method stub
  36. super.onCreate(savedInstanceState);
  37. setContentView(R.layout.contract);
  38. lv = (ListView) findViewById(R.id.addcontracts);

  39. List<AddContract> contracts = readContacts();
  40. Show(contracts,lv);
  41. }


  42. public void Show(List<AddContract> contracts,ListView lv){

  43. ArrayList<Map<String, Object>> al = new ArrayList<Map<String, Object>>();

  44. for (int i = 0; i < contracts.size(); i++) {

  45. Map<String, Object> person = new HashMap<String, Object>();
  46. AddContract c = contracts.get(i);
  47. person.put("name", c.getName());
  48. person.put("phonenumber", c.getNumber());
  49. person.put("pic", R.drawable.ic_launcher);
  50. al.add(person);
  51. }

  52. SimpleAdapter sa = new SimpleAdapter(AddContract.this, al, R.layout.item,
  53. new String[] { "name", "phonenumber", "pic" }, new int[] {
  54. R.id.name, R.id.pnumber, R.id.pic });

  55. lv.setAdapter(sa);

  56. lv.setOnItemClickListener(new OnItemClickListener() {

  57. @Override
  58. public void onItemClick(AdapterView<?> parent, View arg1,
  59. int position, long id) {
  60. // TODO Auto-generated method stub
  61. ListView listView = (ListView) parent;

  62. HashMap<String, Object> itemAtPosition = (HashMap<String, Object>) listView
  63. .getItemAtPosition(position);
  64. HashMap<String, Object> map = itemAtPosition;
  65. //String contactName = (String) map.get("name");
  66. String contactNumber = (String) map.get("phonenumber");
  67. Intent intent = new Intent();
  68. intent.setAction(Intent.ACTION_CALL);// 动作
  69. Uri uri = Uri.parse("tel:" + contactNumber);//
  70. intent.setData(uri);
  71. startActivity(intent);

  72. }
  73. });
  74. }

  75. public List<AddContract> readContacts() {

  76. List<AddContract> contracts = new ArrayList<AddContract>();

  77. Cursor cursor = getContentResolver().query(
  78. ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
  79. // Contacts:这个好像是通讯录里面的一个人的基本描述,像什么显示的名字

  80. while (cursor.moveToNext()) {

  81. HashMap<String, Object> map = new HashMap<String, Object>();
  82. String phoneName = cursor.getString(cursor
  83. .getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
  84. System.out.println(phoneName);
  85. map.put("ItemTitle", phoneName);// 电话姓名

  86. String contactId = cursor.getString(cursor
  87. .getColumnIndex(ContactsContract.Contacts._ID));
  88. System.out.println(contactId);
  89. // 返回结果是String类型,1表示有,0表是没
  90. String hasPhone = cursor
  91. .getString(cursor
  92. .getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
  93. System.out.println(hasPhone);
  94. if (hasPhone.compareTo("1") == 0) {// 如果有电话,根据联系人的ID查找到联系人的电话,电话可以是多个
  95. Cursor phones = getContentResolver().query(
  96. ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
  97. null,
  98. ContactsContract.CommonDataKinds.Phone.CONTACT_ID
  99. + " = " + contactId, null, null);

  100. while (phones.moveToNext()) {
  101. AddContract c = new AddContract();
  102. c.name = phoneName;
  103. String phoneNumber = phones
  104. .getString(phones
  105. .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
  106. String phoneTpye = phones
  107. .getString(phones
  108. .getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));
  109. map.put("ItemText", phoneNumber); // 多个号码如何处理
  110. c.number = phoneNumber;
  111. contracts.add(c);
  112. }

  113. phones.close();
  114. } else {
  115. AddContract c = new AddContract();
  116. c.name = phoneName;
  117. c.number = "";
  118. contracts.add(c);
  119. }
  120. }
  121. return contracts;
  122. }
  123. }
复制代码
下面是ListView的XML文件
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="fill_parent"
  4. android:layout_height="fill_parent"
  5. android:orientation="vertical" >

  6. <ListView
  7. android:id="@+id/addcontracts"
  8. android:layout_width="match_parent"
  9. android:layout_height="wrap_content" >
  10. </ListView>

  11. </LinearLayout>
复制代码
还有item的XML文件
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:orientation="vertical"
  6. android:descendantFocusability="blocksDescendants" >

  7. <RelativeLayout
  8. android:id="@+id/relativeLayout1"
  9. android:layout_width="match_parent"
  10. android:layout_height="wrap_content" >

  11. <ImageView
  12. android:id="@+id/pic"
  13. android:layout_width="wrap_content"
  14. android:layout_height="wrap_content"
  15. android:layout_alignParentTop="true"
  16. android:src="@drawable/ic_launcher" />

  17. <TextView
  18. android:id="@+id/name"
  19. android:layout_width="wrap_content"
  20. android:layout_height="wrap_content"
  21. android:layout_alignParentTop="true"
  22. android:layout_toRightOf="@+id/pic"
  23. android:text="姓名"
  24. android:textSize="20sp" />

  25. <TextView
  26. android:id="@+id/pnumber"
  27. android:layout_width="wrap_content"
  28. android:layout_height="wrap_content"
  29. android:layout_alignBottom="@+id/name"
  30. android:layout_alignParentRight="true"
  31. android:text="号码" />

  32. </RelativeLayout>

  33. </LinearLayout>
复制代码

评分

参与人数 1技术分 +1 收起 理由
王德升 + 1 赞一个!

查看全部评分

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马