Android 最近的联系人获取最近的通话日志时出错

Android 最近的联系人获取最近的通话日志时出错,android,cursor,android-contentprovider,contactscontract,Android,Cursor,Android Contentprovider,Contactscontract,我有一个应用程序,显示最近的电话联系方式。 因此,我使用下面的代码获取最近的联系人,但当我试图运行它时,会出现以下错误 java.lang.IllegalStateException:无法从中读取第0行第1列 游标窗口。在开始之前,请确保光标已正确初始化 从中访问数据 以下是我最近的联系人获取代码: ContentResolver cr = getActivity().getContentResolver(); String[] projection = new String[] {C

我有一个应用程序,显示最近的电话联系方式。 因此,我使用下面的代码获取最近的联系人,但当我试图运行它时,会出现以下错误

java.lang.IllegalStateException:无法从中读取第0行第1列 游标窗口。在开始之前,请确保光标已正确初始化 从中访问数据

以下是我最近的联系人获取代码:

ContentResolver cr = getActivity().getContentResolver();

    String[] projection = new String[] {ContactsContract.Contacts._ID}; // you can add more fields you need here
    int oneDay = ( 1000 *3600 * 24);
    long last24h = (System.currentTimeMillis() - oneDay);

   Cursor cur=cr.query(CallLog.Calls.CONTENT_URI,null,null,null,null);
    String phone = null;
    String emailContact = null;
    String image_uri;
    Bitmap bitmap;


    if (cur.getCount() > 0)
    {
        while (cur.moveToNext())
        {
            String id = cur.getString(cur
                    .getColumnIndex(ContactsContract.Contacts._ID));
            String name = cur
                    .getString(cur
                            .getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));

            image_uri = cur
                    .getString(cur
                              .getColumnIndex(ContactsContract.CommonDataKinds.Phone.PHOTO_URI));
            if (Integer
                    .parseInt(cur.getString(cur
                            .getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0)
            {

                Cursor pCur = cr.query(
                        ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                        null,
                        ContactsContract.CommonDataKinds.Phone.CONTACT_ID
                                + " = ?", new String[]{id}, null);
                while (pCur.moveToNext())
                {
                    phone = pCur
                            .getString(pCur
                                    .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                    // contactid=pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));

                   /* phonenumber.add(pCur
                            .getString(pCur
                                    .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)));`*/

                }
                pCur.close();


                Cursor emailCur = cr.query
                        (
                                ContactsContract.CommonDataKinds.Email.CONTENT_URI,
                                null,
                                ContactsContract.CommonDataKinds.Email.CONTACT_ID
                                        + " = ?", new String[]{id}, null);

                while (emailCur.moveToNext())
                {
                    emailContact = emailCur
                            .getString(emailCur
                                    .getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));

                    if(TextUtils.isEmpty(emailContact)||emailContact.equalsIgnoreCase(null)||emailContact.equalsIgnoreCase(""))
                    {
                        emailContact="";

                    }

                    else
                    {

                    }
                  /*  emailType = emailCur
                            .getString(emailCur
                                    .getColumnIndex(ContactsContract.CommonDataKinds.Email.TYPE));*/
                }
                emailCur.close();
            }

            if (image_uri != null)
            {
                System.out.println(Uri.parse(image_uri));
                try
                {
                    bitmap = MediaStore.Images.Media
                            .getBitmap(getActivity().getContentResolver(),
                                    Uri.parse(image_uri));
                    System.out.println(bitmap);

                } catch (FileNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }

            recent_list.add(new Contacts(name, phone, image_uri,emailContact));
            emailContact="";
            phone="";
        }
        cur.close();
    }
    else
    {
        noContact.setVisibility(View.VISIBLE);
        search_layout.setVisibility(View.GONE);
    }
}

您试图访问光标中未在投影中指定的字段,这是不可能的

投影需要包含所有需要的字段

但是,您无法从呼叫日志表访问联系人字段


就像我最近在这里回答您的另一个问题一样:

您是否授予了必要的权限?您在哪一行收到异常?是的,我提供了所有必需的权限我想要电话上的最近通话记录您在这一行收到异常吗
Cursor cur=cr.query(Contacts contact.Contacts.CONTENT\u URI,null,Contacts contract.Contacts.LAST\u TIME\u contact+“>”+last24h,null,null)?是的,我在指定行上遇到错误请再次检查代码