2014-03-03 168 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