Android获取电话号码

时间:2012-03-10 18:57:45

标签: android database contacts

我正在使用此代码获取每个联系人的电话号码 但我收到以下错误:

  

03-10 19:52:44.925:E / AndroidRuntime(15954):致命异常:主要   03-10 19:52:44.925:E / AndroidRuntime(15954):   java.lang.RuntimeException:无法启动活动   ComponentInfo {contact.backup.alexander.fuchs / contact.backup.alexander.fuchs.App}:   android.database.CursorIndexOutOfBoundsException:索引-1请求,   大小为1 03-10 19:52:44.925:E / AndroidRuntime(15954):at   android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)   03-10 19:52:44.925:E / AndroidRuntime(15954):at   android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)   03-10 19:52:44.925:E / AndroidRuntime(15954):at   android.app.ActivityThread.access $ 1500(ActivityThread.java:117)03-10   19:52:44.925:E / AndroidRuntime(15954):at   android.app.ActivityThread $ H.handleMessage(ActivityThread.java:935)   03-10 19:52:44.925:E / AndroidRuntime(15954):at   android.os.Handler.dispatchMessage(Handler.java:99)03-10   19:52:44.925:E / AndroidRuntime(15954):at   android.os.Looper.loop(Looper.java:130)03-10 19:52:44.925:   E / AndroidRuntime(15954):at   android.app.ActivityThread.main(ActivityThread.java:3691)03-10   19:52:44.925:E / AndroidRuntime(15954):at   java.lang.reflect.Method.invokeNative(Native Method)03-10   19:52:44.925:E / AndroidRuntime(15954):at   java.lang.reflect.Method.invoke(Method.java:507)03-10 19:52:44.925:   E / AndroidRuntime(15954):at   com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:907)   03-10 19:52:44.925:E / AndroidRuntime(15954):at   com.android.internal.os.ZygoteInit.main(ZygoteInit.java:665)03-10   19:52:44.925:E / AndroidRuntime(15954):at   dalvik.system.NativeStart.main(Native Method)03-10 19:52:44.925:   E / AndroidRuntime(15954):引起:   android.database.CursorIndexOutOfBoundsException:索引-1请求,   大小为1 03-10 19:52:44.925:E / AndroidRuntime(15954):at   android.database.AbstractCursor.checkPosition(AbstractCursor.java:580)   03-10 19:52:44.925:E / AndroidRuntime(15954):at   android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:214)   03-10 19:52:44.925:E / AndroidRuntime(15954):at   android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:41)   03-10 19:52:44.925:E / AndroidRuntime(15954):at   android.database.CursorWrapper.getString(CursorWrapper.java:135)03-10   19:52:44.925:E / AndroidRuntime(15954):at   contact.backup.alexander.fuchs.App.onCreate(App.java:85)03-10   19:52:44.925:E / AndroidRuntime(15954):at   android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)   03-10 19:52:44.925:E / AndroidRuntime(15954):at   android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)   03-10 19:52:44.925:E / AndroidRuntime(15954):... 11 more

我的代码:

package contact.backup.alexander.fuchs;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.ContentUris;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.Contacts.People;
import android.provider.Contacts.Phones;
import android.provider.ContactsContract;

public class App extends Activity {
    /** Called when the activity is first created. */

    int i;
    int i2;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);


        util.deleteDir(new File("/data/data/contact.backup.alexander.fuchs/backup/"));
        new File("/data/data/contact.backup.alexander.fuchs/backup/").mkdirs(); 



        // get it 
        ContentResolver cr = getContentResolver();
        Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,
                null, null, null, null);


        if (cur.getCount() > 0) {
            i = 0;
            i2 = 0;
        while (cur.moveToNext()) {
            String id = cur.getString(
                        cur.getColumnIndex(ContactsContract.Contacts._ID));
        String name = cur.getString(
                        cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));


        // write
        try
        {
            new File("/data/data/contact.backup.alexander.fuchs/backup/"+String.valueOf(i)).mkdirs(); 

            FileWriter fstream = new FileWriter("/data/data/contact.backup.alexander.fuchs/backup/"+String.valueOf(i)+"/id.txt");
            BufferedWriter out = new BufferedWriter(fstream);
            out.write(id);
            //Close the output stream
            out.close();


            fstream = new FileWriter("/data/data/contact.backup.alexander.fuchs/backup/"+String.valueOf(i)+"/name.txt");
            out = new BufferedWriter(fstream);
            out.write(name);
            //Close the output stream
            out.close();





        }
        catch (Exception x)
        {

        }
        if (Integer.parseInt(cur.getString(
                cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
             Cursor pCur = cr.query(
            ContactsContract.CommonDataKinds.Phone.CONTENT_URI, 
            null, 
            ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?", 
            new String[]{id}, null);

                String number = pCur.getString(
                        pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                while (pCur.moveToNext()) {
                    try
                    {
                    new File("/data/data/contact.backup.alexander.fuchs/backup/"+String.valueOf(id)).mkdirs(); 

                    FileWriter fstream = new FileWriter("/data/data/contact.backup.alexander.fuchs/backup/"+String.valueOf(id)+"/number.txt");
                    BufferedWriter out = new BufferedWriter(fstream);
                    out.write(number);
                    //Close the output stream
                    out.close();


                    }
                    catch(Exception x)
                    {

                    }
                    } 


            pCur.close();

        }
        i++;
        }


    }

    }
}

1 个答案:

答案 0 :(得分:0)

 private ArrayList<Map<String, String>> peopleList;
 private AutoCompleteTextView txtPhoneName;
    private SimpleAdapter mAdapter;

//在oncreate中

 txtPhoneName=(AutoCompleteTextView)findViewById(R.id.autoDeni);

peopleList = new ArrayList<Map<String, String>>();
PopulatePeopleList();


mAdapter = new SimpleAdapter(this, peopleList, R.layout.row ,new String[] { "Name", "Phone"  }, new int[] { R.id.text1, R.id.text2  });

txtPhoneName.setAdapter(mAdapter);

 public void PopulatePeopleList()
  {

 peopleList.clear();

Cursor people = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);

while (people.moveToNext())
{
String contactName = people.getString(people.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));

String contactId = people.getString(people.getColumnIndex(ContactsContract.Contacts._ID));
String hasPhone = people.getString(people.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));

if ((Integer.parseInt(hasPhone) > 0))
{

// You know have the number so now query it like this
Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ contactId,null, null);
while (phones.moveToNext()) {

//store numbers and display a dialog letting the user select which.
String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));

//String numberType = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));

Map<String, String> mapa = new HashMap<String, String>();

mapa.put("Name",contactName+"\n"+phoneNumber);
mapa.put("Phone", phoneNumber);

/* if(numberType.equals("0"))
mapa.put("Type", "Work");
else
if(numberType.equals("1"))
mapa.put("Type", "Home");
else if(numberType.equals("2"))
mapa.put("Type",  "Mobile");
else
mapa.put("Type", "Other");*/

//Then add this map to the list.
peopleList.add(mapa);
}
phones.close();
}
}
people.close();

startManagingCursor(people);
}

//这是我获取autocompletetextview中所有联系人的方法我将在简单的适配器中显示我正在使用的row.xml注意:在oncreate中当前上下文不是row.xml

行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="horizontal" >
 <TextView 
android:id="@+id/text1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" 
android:padding="10dip" >


</TextView>

<TextView 
android:id="@+id/text2"
android:layout_width="fill_parent"
android:layout_height="fill_parent" 
android:padding="10dip" >


</TextView>

相关问题