1

我在光標moveToFirst()獲取空指針異常時檢索聯繫人電話號碼在棒棒糖設備上,但在其他操作系統版本工作正常。Android Lollipop聯繫人檢索空指針異常

異常發生在pCur.moveToFirst(); under the getContact() method

請參閱我的代碼:

public class MyService extends Service { 

    public static Context mContext; 
    LinkedHashMap<String, String> name = new LinkedHashMap<String, String>(); 
    HashMap<String, String> contactDetails = new HashMap<String, String>(); 
    HashMap<String, Bitmap> image = new HashMap<String, Bitmap>(); 
    private Cursor pCur, contactsCursor; 

    @Override 
    public int onStartCommand(Intent intent, int flags, int startId) { 

     String[] PROJECTION = { Contacts._ID, Contacts.LOOKUP_KEY, 
       Contacts.DISPLAY_NAME_PRIMARY, Contacts.PHOTO_THUMBNAIL_URI, 
       Contacts.SORT_KEY_PRIMARY }; 

     String SELECTION = Contacts.DISPLAY_NAME_PRIMARY + "<>''" + " AND " 
       + Contacts.IN_VISIBLE_GROUP + "=1" + " AND " 
       + Contacts.HAS_PHONE_NUMBER; 
     String SORT_ORDER = Contacts.SORT_KEY_ALTERNATIVE; 
     contactsCursor = getContentResolver().query(Contacts.CONTENT_URI, 
       PROJECTION, SELECTION, null, SORT_ORDER); 
     StoreCursor.qcursor = contactsCursor; 
     Log.e("cur", "cur" + StoreCursor.qcursor.getCount()); 
     getContact(); 
     return START_STICKY; 
    } 

    private void getContact() { 
     Log.e("result cursor", "" + contactsCursor.getCount()); 
     String displayName; 
     String contactId; 
     contactsCursor.moveToFirst(); 
     do { 
      displayName = contactsCursor.getString(2); 
      contactId = contactsCursor.getString(0); 
      // Log.e("disName & id", displayName + " "+contactId); 
      pCur = getContentResolver().query(
        ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, 
        ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", 
        new String[] { contactId }, null); 
      String cPN = ""; 

       pCur.moveToFirst(); // NullPointerException occur here. 
       do { 
        int phoneType = pCur 
          .getInt(pCur 
            .getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE)); 
        String phoneNumber = pCur 
          .getString(pCur 
            .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); 
        switch (phoneType) { 
        case Phone.TYPE_MOBILE: 
         cPN = cPN + "(mobile number)" + phoneNumber + "\n"; 
         break; 
        case Phone.TYPE_HOME: 
         cPN = cPN + "(home number)" + phoneNumber + "\n"; 
         break; 
        case Phone.TYPE_WORK: 
         cPN = cPN + "(work number)" + phoneNumber + "\n"; 
         break; 
        case Phone.TYPE_OTHER: 
         cPN = cPN + "(other number)" + phoneNumber + "\n"; 
         break; 

        default: 
         break; 
        } 

       } while (pCur.moveToNext()); 
       name.put(contactId, displayName); 
       Log.e("displayName", displayName); 
       StoreCursor.name = name; 
       contactDetails.put(contactId, cPN); 
       StoreCursor.contactDetails = contactDetails; 
       String photo = contactsCursor.getString(3) + "~"; 
       // Log.e("photo url", photo); 
       if (photo.length() > 6) { 
        openPhoto(Long.valueOf(contactId), displayName); 
       } 

     } while (contactsCursor.moveToNext()); 

    } 

    @Override 
    public void onDestroy() { 
     super.onDestroy(); 
    } 
} 

回答

1

這是一個容易:

如果pCur.moveToFirst();拋出一個NullPointerException,這意味着pCurnull。由於pCur只在一個地方設置,

pCur = getContentResolver().query(...); 

,這意味着您的來電ContentResolver.query回報null

現在的real問題是:爲什麼ContentResolver.query返回null?這是一個很好的問題,我建議你在SO上提出這是一個新問題。一定要包括所有相關的信息,即:

  • 你想解決哪些URL,
  • 參數你用何地,你從得到他們,

理想情況下,您應該爲您的新問題添加minimal complete example(請注意,您當前的代碼片段既不小也不完整)。

+0

好像選擇和排序順序查詢不是合成正確的,這會產生不兼容的SQL。 –