我的程序是获得手机的通讯录,为什么会出现空指向异常?- package imut.chenying;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- import android.app.Activity;
- import android.content.Intent;
- import android.database.Cursor;
- import android.net.Uri;
- import android.os.Bundle;
- import android.provider.ContactsContract;
- import android.view.View;
- import android.widget.AdapterView;
- import android.widget.AdapterView.OnItemClickListener;
- import android.widget.ListView;
- import android.widget.SimpleAdapter;
- public class AddContract extends Activity {
- private String name;
- private String number;
- ListView lv;
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public String getNumber() {
- return number;
- }
- public void setNumber(String number) {
- this.number = number;
- }
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- // TODO Auto-generated method stub
- super.onCreate(savedInstanceState);
- setContentView(R.layout.contract);
- lv = (ListView) findViewById(R.id.addcontracts);
- List<AddContract> contracts = readContacts();
- Show(contracts,lv);
- }
- public void Show(List<AddContract> contracts,ListView lv){
- ArrayList<Map<String, Object>> al = new ArrayList<Map<String, Object>>();
- for (int i = 0; i < contracts.size(); i++) {
- Map<String, Object> person = new HashMap<String, Object>();
- AddContract c = contracts.get(i);
- person.put("name", c.getName());
- person.put("phonenumber", c.getNumber());
- person.put("pic", R.drawable.ic_launcher);
- al.add(person);
- }
- SimpleAdapter sa = new SimpleAdapter(AddContract.this, al, R.layout.item,
- new String[] { "name", "phonenumber", "pic" }, new int[] {
- R.id.name, R.id.pnumber, R.id.pic });
- lv.setAdapter(sa);
- lv.setOnItemClickListener(new OnItemClickListener() {
- @Override
- public void onItemClick(AdapterView<?> parent, View arg1,
- int position, long id) {
- // TODO Auto-generated method stub
- ListView listView = (ListView) parent;
- HashMap<String, Object> itemAtPosition = (HashMap<String, Object>) listView
- .getItemAtPosition(position);
- HashMap<String, Object> map = itemAtPosition;
- //String contactName = (String) map.get("name");
- String contactNumber = (String) map.get("phonenumber");
- Intent intent = new Intent();
- intent.setAction(Intent.ACTION_CALL);// 动作
- Uri uri = Uri.parse("tel:" + contactNumber);//
- intent.setData(uri);
- startActivity(intent);
- }
- });
- }
- public List<AddContract> readContacts() {
- List<AddContract> contracts = new ArrayList<AddContract>();
- Cursor cursor = getContentResolver().query(
- ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
- // Contacts:这个好像是通讯录里面的一个人的基本描述,像什么显示的名字
- while (cursor.moveToNext()) {
- HashMap<String, Object> map = new HashMap<String, Object>();
- String phoneName = cursor.getString(cursor
- .getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
- System.out.println(phoneName);
- map.put("ItemTitle", phoneName);// 电话姓名
- String contactId = cursor.getString(cursor
- .getColumnIndex(ContactsContract.Contacts._ID));
- System.out.println(contactId);
- // 返回结果是String类型,1表示有,0表是没
- String hasPhone = cursor
- .getString(cursor
- .getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
- System.out.println(hasPhone);
- if (hasPhone.compareTo("1") == 0) {// 如果有电话,根据联系人的ID查找到联系人的电话,电话可以是多个
- Cursor phones = getContentResolver().query(
- ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
- null,
- ContactsContract.CommonDataKinds.Phone.CONTACT_ID
- + " = " + contactId, null, null);
- while (phones.moveToNext()) {
- AddContract c = new AddContract();
- c.name = phoneName;
- String phoneNumber = phones
- .getString(phones
- .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
- String phoneTpye = phones
- .getString(phones
- .getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));
- map.put("ItemText", phoneNumber); // 多个号码如何处理
- c.number = phoneNumber;
- contracts.add(c);
- }
- phones.close();
- } else {
- AddContract c = new AddContract();
- c.name = phoneName;
- c.number = "";
- contracts.add(c);
- }
- }
- return contracts;
- }
- }
复制代码 下面是ListView的XML文件- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="vertical" >
- <ListView
- android:id="@+id/addcontracts"
- android:layout_width="match_parent"
- android:layout_height="wrap_content" >
- </ListView>
- </LinearLayout>
复制代码 还有item的XML文件- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical"
- android:descendantFocusability="blocksDescendants" >
- <RelativeLayout
- android:id="@+id/relativeLayout1"
- android:layout_width="match_parent"
- android:layout_height="wrap_content" >
- <ImageView
- android:id="@+id/pic"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignParentTop="true"
- android:src="@drawable/ic_launcher" />
- <TextView
- android:id="@+id/name"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignParentTop="true"
- android:layout_toRightOf="@+id/pic"
- android:text="姓名"
- android:textSize="20sp" />
- <TextView
- android:id="@+id/pnumber"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignBottom="@+id/name"
- android:layout_alignParentRight="true"
- android:text="号码" />
- </RelativeLayout>
- </LinearLayout>
复制代码 |