2011-05-30 22 views
0

時遇到錯誤,我正在嘗試檢索電話簿的聯繫人。它似乎查詢沒有返回任何結果,雖然我有存儲在模擬器上的聯繫人。我用下面的代碼:我在嘗試檢索我的應用程序中的聯繫人

Cursor people = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null); 
    people.moveToFirst(); 
    while(people.moveToNext()) { 
     int nameFieldColumnIndex = people.getColumnIndex(PhoneLookup.DISPLAY_NAME); 
     String contact = people.getString(nameFieldColumnIndex); 
     int numberFieldColumnIndex = people.getColumnIndex(PhoneLookup.NUMBER); 
     String number = people.getString(numberFieldColumnIndex); 
     Toast.makeText(this, ""+contact+""+number, Toast.LENGTH_LONG); 
    } 

    people.close(); 

錯誤的日誌

E/AndroidRuntime( 277): FATAL EXCEPTION: main 

E/AndroidRuntime( 277): java.lang.RuntimeException: Unable to start service [email protected] with Intent { cmp=mobile_security.mobile_security.ui/.getSimCardContacts }: java.lang.IllegalStateException: get field slot from row 1 col -1 failed 

E/AndroidRuntime( 277): at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3063) 

E/AndroidRuntime( 277): at android.app.ActivityThread.access$3600(ActivityThread.java:125) 

E/AndroidRuntime( 277): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2096) 

E/AndroidRuntime( 277): at android.os.Handler.dispatchMessage(Handler.java:99) 

E/AndroidRuntime( 277): at android.os.Looper.loop(Looper.java:123) 

E/AndroidRuntime( 277): at android.app.ActivityThread.main(ActivityThread.java:4627) 

E/AndroidRuntime( 277): at java.lang.reflect.Method.invokeNative(Native Method) 

E/AndroidRuntime( 277): at java.lang.reflect.Method.invoke(Method.java:521) 

E/AndroidRuntime( 277): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 

E/AndroidRuntime( 277): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 

E/AndroidRuntime( 277): at dalvik.system.NativeStart.main(Native Method) 

E/AndroidRuntime( 277): Caused by: java.lang.IllegalStateException: get field slot from row 1 col -1 failed 

E/AndroidRuntime( 277): at android.database.CursorWindow.getString_native(Native Method) 

E/AndroidRuntime( 277): at android.database.CursorWindow.getString(CursorWindow.java:329) 

E/AndroidRuntime( 277): at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:49) 

E/AndroidRuntime( 277): at android.database.CursorWrapper.getString(CursorWrapper.java:135) 

E/AndroidRuntime( 277): at mobile_security.mobile_security.ui.getSimCardContacts.onStart(getSimCardContacts.java:40) 

E/AndroidRuntime( 277): at android.app.Service.onStartCommand(Service.java:420) 

E/AndroidRuntime( 277): at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3053) 

E/AndroidRuntime( 277): ... 10 more 

我不知道該如何處理這個問題。任何人都可以幫忙THX

回答

0

使用下面的代碼從手機獲得聯繫人

public static void getContactNumbers(Context context) { 
     String contactNumber = null; 
     int contactNumberType = Phone.TYPE_MOBILE; 
     String nameOfContact = null; 
     if (ApplicationConstants.phoneContacts.size() <= 0) { 
      ContentResolver cr = context.getContentResolver(); 
      Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, 
        null, null, null); 
      if (cur.getCount() > 0) { 
       while (cur.moveToNext()) { 
        String id = cur.getString(cur 
          .getColumnIndex(BaseColumns._ID)); 
        nameOfContact = cur 
          .getString(cur 
            .getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)); 

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

         while (phones.moveToNext()) { 
          contactNumber = phones.getString(phones 
            .getColumnIndex(Phone.NUMBER)); 
          contactNumberType = phones.getInt(phones 
            .getColumnIndex(Phone.TYPE)); 
          Log.i(TAG, "...Contact Name ...." + nameOfContact 
            + "...contact Number..." + contactNumber); 

         } 
         phones.close(); 
        } 

       } 
      }// end of contact name cursor 
      cur.close(); 

     } 
    } 

請參閱以下網址。 getting all phone book contact details into an array in android

感謝 迪帕克

+0

哎以及ApplicationConstants不能得到解決。有沒有我需要進口的減稅? – wassim 2011-05-30 05:15:07

相關問題