【问题标题】:Android getting phonenumberAndroid获取电话号码
【发布时间】:2012-03-10 18:57:44
【问题描述】:

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

03-10 19:52:44.925:E/AndroidRuntime(15954):致命异常:主要 03-10 19:52:44.925: E/AndroidRuntime(15954): java.lang.RuntimeException:无法启动活动 组件信息{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): 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667) 03-10 19:52:44.925: E/AndroidRuntime(15954): 在 android.app.ActivityThread.access$1500(ActivityThread.java:117) 03-10 19:52:44.925:E/AndroidRuntime(15954):在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:935) 03-10 19:52:44.925: E/AndroidRuntime(15954): 在 android.os.Handler.dispatchMessage(Handler.java:99) 03-10 19:52:44.925:E/AndroidRuntime(15954):在 android.os.Looper.loop(Looper.java:130) 03-10 19:52:44.925: E/AndroidRuntime(15954):在 android.app.ActivityThread.main(ActivityThread.java:3691) 03-10 19:52:44.925:E/AndroidRuntime(15954):在 java.lang.reflect.Method.invokeNative(Native Method) 03-10 19:52:44.925:E/AndroidRuntime(15954):在 java.lang.reflect.Method.invoke(Method.java:507) 03-10 19:52:44.925: E/AndroidRuntime(15954):在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:907) 03-10 19:52:44.925: E/AndroidRuntime(15954): 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:665) 03-10 19:52:44.925:E/AndroidRuntime(15954):在 dalvik.system.NativeStart.main(本机方法)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): 在 android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:214) 03-10 19:52:44.925: E/AndroidRuntime(15954): 在 android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:41) 03-10 19:52:44.925: E/AndroidRuntime(15954): 在 android.database.CursorWrapper.getString(CursorWrapper.java:135) 03-10 19:52:44.925:E/AndroidRuntime(15954):在 contact.backup.alexander.fuchs.App.onCreate(App.java:85) 03-10 19:52:44.925:E/AndroidRuntime(15954):在 android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 03-10 19:52:44.925: E/AndroidRuntime(15954): 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615) 03-10 19:52:44.925: E/AndroidRuntime(15954): ... 11 更多

我的代码:

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++;
        }


    }

    }
}

【问题讨论】:

    标签: android database contacts


    【解决方案1】:
     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);
    }
    

    //这是我在自动完成文本视图中获取所有联系人的结果,在这里我将向您展示我在简单适配器中使用的 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>
    

    【讨论】:

      猜你喜欢
      • 2013-11-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-23
      • 1970-01-01
      • 1970-01-01
      • 2015-07-22
      相关资源
      最近更新 更多