2014-03-03 138 views
0
Cursor people = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null); 
while (people.moveToNext()){ 
    int NameIndex = people.getColumnIndex(PhoneLookup.DISPLAY_NAME); 
    int NumIndex = people.getColumnIndex(PhoneLookup.NUMBER); 

    String Name = people.getString(NameIndex); 
    //String Num = people.getString(NumIndex); 

    myArr.add(Name.toString()); 
    myNum.add(Num.toString()); 

} 

你好我遇到的问题检索联系人的电话号码,每次我让这行代码越来越联系人电话号码

String Num = people.getString(NumIndex); 
03-03 15:38:30.436: E/AndroidRuntime(496): FATAL EXCEPTION: main 
03-03 15:38:30.436: E/AndroidRuntime(496): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.GhattasAk.RingMe/com.GhattasAk.RingMe.Main}: java.lang.IllegalStateException: get field slot from row 0 col -1 failed 
03-03 15:38:30.436: E/AndroidRuntime(496): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647) 
03-03 15:38:30.436: E/AndroidRuntime(496): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663) 
03-03 15:38:30.436: E/AndroidRuntime(496): at android.app.ActivityThread.access$1500(ActivityThread.java:117) 
03-03 15:38:30.436: E/AndroidRuntime(496): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931) 
03-03 15:38:30.436: E/AndroidRuntime(496): at android.os.Handler.dispatchMessage(Handler.java:99) 
03-03 15:38:30.436: E/AndroidRuntime(496): at android.os.Looper.loop(Looper.java:123) 
03-03 15:38:30.436: E/AndroidRuntime(496): at android.app.ActivityThread.main(ActivityThread.java:3683) 
03-03 15:38:30.436: E/AndroidRuntime(496): at java.lang.reflect.Method.invokeNative(Native Method) 
03-03 15:38:30.436: E/AndroidRuntime(496): at java.lang.reflect.Method.invoke(Method.java:507) 
03-03 15:38:30.436: E/AndroidRuntime(496): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) 
03-03 15:38:30.436: E/AndroidRuntime(496): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 
03-03 15:38:30.436: E/AndroidRuntime(496): at dalvik.system.NativeStart.main(Native Method) 
03-03 15:38:30.436: E/AndroidRuntime(496): Caused by: java.lang.IllegalStateException: get field slot from row 0 col -1 failed 
03-03 15:38:30.436: E/AndroidRuntime(496): at android.database.CursorWindow.getString_native(Native Method) 
03-03 15:38:30.436: E/AndroidRuntime(496): at android.database.CursorWindow.getString(CursorWindow.java:329) 
03-03 15:38:30.436: E/AndroidRuntime(496): at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:49) 
03-03 15:38:30.436: E/AndroidRuntime(496): at android.database.CursorWrapper.getString(CursorWrapper.java:135) 
03-03 15:38:30.436: E/AndroidRuntime(496): at com.GhattasAk.RingMe.Main.onCreate(Main.java:49) 
03-03 15:38:30.436: E/AndroidRuntime(496): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 
03-03 15:38:30.436: E/AndroidRuntime(496): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611) 
03-03 15:38:30.436: E/AndroidRuntime(496): ... 11 more 

应用程序崩溃。我不明白为什么

这是编辑根据数字得到一个名字是真的吗?

   if(state==TelephonyManager.CALL_STATE_RINGING) 
       { 
       // Find contact based on name. 
        ContentResolver cr = getContentResolver(); 
        Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null, 
         "NUMBER = '" + number + "'", null, null); 
        if (cursor.moveToFirst()) { 
         String contactId = 
          cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID)); 
         // Get all phone numbers. 
         Cursor phones = cr.query(Phone.CONTENT_URI, null, 
          Phone.CONTACT_ID + " = " + contactId, null, null); 
         while (phones.moveToNext()) { 
          String numb = phones.getString(phones.getColumnIndex(Phone.DISPLAY_NAME)); 
         } 
         phones.close(); 
        } 
        cursor.close(); 
+0

安置自己的logcat的错误消息。 – InnocentKiller

+0

好吧我编辑帖子 – user3368764

回答

0

大概becouse联系人有多个电话NRS喜欢的工作...... 试试这个:

// Find contact based on name. 
     // 
     ContentResolver cr = getContentResolver(); 
     Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null, 
      "DISPLAY_NAME = '" + NAME + "'", null, null); 
     if (cursor.moveToFirst()) { 
      String contactId = 
       cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID)); 
      // 
      // Get all phone numbers. 
      // 
      Cursor phones = cr.query(Phone.CONTENT_URI, null, 
       Phone.CONTACT_ID + " = " + contactId, null, null); 
      while (phones.moveToNext()) { 
       String number = phones.getString(phones.getColumnIndex(Phone.NUMBER)); 
       int type = phones.getInt(phones.getColumnIndex(Phone.TYPE)); 
       switch (type) { 
        case Phone.TYPE_HOME: 
         // do something with the Home number here... 
         break; 
        case Phone.TYPE_MOBILE: 
         // do something with the Mobile number here... 
         break; 
        case Phone.TYPE_WORK: 
         // do something with the Work number here... 
         break; 
        } 
      } 
      phones.close(); 
     } 
    cursor.close(); 
+0

联系人在模拟器上,我只添加了1个编号 – user3368764

+0

从第0行获得字段插槽col -1显示当android无法从db中找到信息到单元格中,所以请让我的方法并尝试看看它是否工作正常 –

+0

好吧,只需一秒钟,所以我更新这个检索名称基于主叫号码 – user3368764