3

我已经构建了以下方法来使用从 Contacts 表中获取的 id 从 RawContacts 表中获取 id。此方法因异常而失败。

     public int getRawContactId(int contactId)
{
    String[] projection=new String[]{ContactsContract.RawContacts._ID};
    String selection=ContactsContract.RawContacts.CONTACT_ID+"=?";
    String[] selectionArgs=new String[]{String.valueOf(contactId)};
    Cursor c=context.getContentResolver().query(ContactsContract.RawContacts.CONTENT_URI,projection,selection,selectionArgs , null);
    int rawContactId=c.getInt(c.getColumnIndex(ContactsContract.RawContacts._ID));
    Log.d(TAG,"Contact Id: "+contactId+" Raw Contact Id: "+rawContactId);
    return rawContactId;
}

对应的 Logcat 如下所示:

           FATAL EXCEPTION: main
           java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, 
           request=1, result=-1, data=Intent {  
           dat=content://com.android.contacts/contacts/lookup/3844i21/993 flg=0x1 }} to    
           activity {com.example.contactbadger/com.example.contactbadger.MainActivity}: 
           android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a  
           size of 1
           at android.app.ActivityThread.deliverResults(ActivityThread.java:3367)
        at android.app.ActivityThread.handleSendResult(ActivityThread.java:3410)
           at android.app.ActivityThread.access$1100(ActivityThread.java:141)
           at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1304)
           at android.os.Handler.dispatchMessage(Handler.java:99)
           at android.os.Looper.loop(Looper.java:137)
           at android.app.ActivityThread.main(ActivityThread.java:5103)
           at java.lang.reflect.Method.invokeNative(Native Method)
           at java.lang.reflect.Method.invoke(Method.java:525)
           at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
           at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
           at dalvik.system.NativeStart.main(Native Method)
           Caused by: android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 1
           at android.database.AbstractCursor.checkPosition(AbstractCursor.java:424)
           at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136)
           at android.database.AbstractWindowedCursor.getInt(AbstractWindowedCursor.java:68)
           at android.database.CursorWrapper.getInt(CursorWrapper.java:102)
           at com.example.contactbadger.ContactUtils.getRawContactId(ContactUtils.java:171)
           at com.example.contactbadger.MainActivity.getContactPhoto(MainActivity.java:65)
           at com.example.contactbadger.MainActivity.onActivityResult(MainActivity.java:104)
           at android.app.Activity.dispatchActivityResult(Activity.java:5322)
           at android.app.ActivityThread.deliverResults(ActivityThread.java:3363)
4

2 回答 2

5

您需要将光标移动到第一个位置。默认情况下,光标设置在 -1 索引处,因此您需要使用moveToFirst()将其移动到第 0 个位置。


所以你可以修改你的代码

Cursor c = context.getContentResolver().query(ContactsContract.RawContacts.CONTENT_URI,projection,selection,selectionArgs , null);    
if (c.moveToFirst()) {    
    int rawContactId=c.getInt(c.getColumnIndex(ContactsContract.RawContacts._ID));
}
于 2013-10-30T06:28:32.037 回答
1
public static String getRawContactId(String contactId)
{
    String res = "";
    Uri uri = ContactsContract.RawContacts.CONTENT_URI;
    String[] projection = new String[]{ContactsContract.RawContacts._ID};
    String selection = ContactsContract.RawContacts.CONTACT_ID + " = ?";
    String[] selectionArgs = new String[]{ contactId };
    Cursor c = ContentManager.getContentResolver().query(uri, projection, selection, selectionArgs, null);

    if(c != null && c.moveToFirst())
    {
        res = c.getString(c.getColumnIndex(ContactsContract.RawContacts._ID));
        c.close();
    }

    return res;
}
于 2017-03-12T10:11:14.827 回答