Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/205.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
检索给定ID的联系人信息(Android)_Android - Fatal编程技术网

检索给定ID的联系人信息(Android)

检索给定ID的联系人信息(Android),android,Android,我正试图根据其ID或查找检索特定的联系信息(如电话号码、姓名和电子邮件) 首先,我使用此代码读取所有当前联系人数据: ContentResolver contentResolver = getContentResolver(); cursor = contentResolver.query(ContactsContract.Contacts.CONTENT_URI,null,null,null, ContactsContract.Contacts.SORT_KEY

我正试图根据其
ID
查找
检索特定的联系信息(如电话号码、姓名和电子邮件)

首先,我使用此代码读取所有当前联系人数据:

ContentResolver contentResolver = getContentResolver();
    cursor = contentResolver.query(ContactsContract.Contacts.CONTENT_URI,null,null,null,
            ContactsContract.Contacts.SORT_KEY_PRIMARY + " ASC");
一旦用户选择了一个特定的联系人,我就通过putExtra方法将其
id
传递给另一个活动,该活动旨在读取和显示该联系人的信息

Intent contactDetails = new Intent(ContactsMainActivity.this, ContactsDialogActivity.class);
contactDetails.putExtra("ID",id);
startActivity(contactDetails);
问题就出现在这里。在
ContactsDialogActivity
中,我想通过搜索
id
来检索该联系人的信息:

cursor = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI,null, 
                ContactsContract.Contacts._ID + " = ?", new String[]{String.valueOf(id)},
                ContactsContract.Contacts.SORT_KEY_PRIMARY + " ASC");
但该查询总是向我抛出一个-1索引(它找不到该ID),返回以下错误:

07-17 11:23:14.637 27073-27073/gib.bcimobilephone E/AndroidRuntime: FATAL EXCEPTION: main
                                                                Process: gib.bcimobilephone, PID: 27073
                                                                java.lang.RuntimeException: Unable to start activity ComponentInfo{gib.bcimobilephone/gib.bcimobilephone.ContactsDialogActivity}: android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 1
                                                                    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2924)
                                                                    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2985)
                                                                    at android.app.ActivityThread.-wrap14(ActivityThread.java)
                                                                    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1635)
                                                                    at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                    at android.os.Looper.loop(Looper.java:154)
                                                                    at android.app.ActivityThread.main(ActivityThread.java:6692)
                                                                    at java.lang.reflect.Method.invoke(Native Method)
                                                                    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1468)
                                                                    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1358)
                                                                 Caused by: android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 1
                                                                    at android.database.AbstractCursor.checkPosition(AbstractCursor.java:460)
                                                                    at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136)
                                                                    at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:50)
                                                                    at android.database.CursorWrapper.getString(CursorWrapper.java:137)
                                                                    at gib.bcimobilephone.ContactsDialogActivity.onCreate(ContactsDialogActivity.java:94)
                                                                    at android.app.Activity.performCreate(Activity.java:6912)
                                                                    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1126)
                                                                    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2877)
                                                                    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2985) 
                                                                    at android.app.ActivityThread.-wrap14(ActivityThread.java) 
                                                                    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1635) 
                                                                    at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                    at android.os.Looper.loop(Looper.java:154) 
                                                                    at android.app.ActivityThread.main(ActivityThread.java:6692) 
                                                                    at java.lang.reflect.Method.invoke(Native Method) 
                                                                    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1468) 
                                                                    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1358) 
我做错了什么?

假设您有一个有效的contactID,您可以这样做:
Assuming you have a valid contactID, you can do this:

Cursor result = managedQuery(ContactsContract.Contacts.CONTENT_URI, null, 
         ContactsContract.Contacts._ID +" = ?", 
         new String[]{contactID}, null);      
if (result.moveToFirst()) {

    for(int i=0; i< result.getColumnCount(); i++){
        Log.i("CONTACTSTAG", result.getColumnName(i) + ": "
        + result.getString(i));
    }         
}
You will have to change the ContactsContract.Contacts.CONTENT_URI and the where clause to the table that you are querying. The above code will print out a bunch of general info about a contact.
游标结果=managedQuery(ContactsContract.Contacts.CONTENT\u URI,null, 联系人合同联系人。\u ID+“=?”, 新字符串[]{contactID},null); if(result.moveToFirst()){ for(int i=0;i
听起来您的查询与所有元素都不匹配<如果未找到任何内容,则通常会返回代码>-1。在尝试访问该索引位置处的数组之前,您应该检查它是否为
-1
。但是我认为我没有正确地进行搜索,因为该ID以前是由第一个查询返回的。