2017-06-16 73 views
0

我有一個應用程序顯示手機的最近聯繫人。 所以我用下面的代碼來獲取最近的聯繫人,但是當我試圖運行它給我下面的錯誤。獲取最近通話記錄的最近聯繫人給出錯誤

java.lang.IllegalStateException:無法讀取行0,列-1從 CursorWindow。確保光標在 從其訪問數據之前正確初始化。

這是我最近的聯繫人獲取代碼:

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); 
    } 
} 
+0

你給了必要的權限嗎?你在哪一行得到異常? –

+0

是的,我給了所有必要的權限 –

+0

我想在電話上最近的通話記錄 –

回答

0

你試圖訪問那些沒有在投影指定光標領域,這是不可能的。

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

然而,您不能從呼叫日誌表中訪問聯繫人字段。

就像我最近在這裏回答你的其他問題:How to fetch recent and favourite contacts in android?