2015-06-20 46 views
0

我正在訪問聯繫人的姓名,電話號碼和照片。一切工作正常,但如果聯繫人沒有分配任何照片,那麼該應用程序拋出一個錯誤並強制關閉。 我有三種方法來檢索名稱,數字和照片,我用這些數據來設置2個editTexts和一個imageView。拾取沒有照片ID /顯示的聯繫人時出錯圖片

private void retrieveContactPhoto() { 

    Bitmap photo = null; 

    try { 


     ImageView imageView = (ImageView) findViewById(R.id.callerPic); 
     imageView.setImageBitmap(photo); 
     InputStream inputStream = ContactsContract.Contacts.openContactPhotoInputStream(getContentResolver(), 
       ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, new Long(contactID))); 

     if (inputStream != null) { 
      photo = BitmapFactory.decodeStream(inputStream); 
      // ImageView imageView = (ImageView) findViewById(R.id.callerPic); 
      imageView.setImageBitmap(photo); 
     } 
     if (inputStream == null) { 
      Toast.makeText(getApplicationContext(), "No profile pic found", Toast.LENGTH_LONG).show(); 
     } 

     assert inputStream != null; 
     inputStream.close(); 

    } catch (IOException e) { 
     e.printStackTrace(); 
    } 

} 

private void retrieveContactNumber() { 

    String contactNumber = null; 

    // getting contacts ID 
    Cursor cursorID = getContentResolver().query(uriContact, 
      new String[]{ContactsContract.Contacts._ID}, 
      null, null, null); 

    if (cursorID.moveToFirst()) { 

     contactID = cursorID.getString(cursorID.getColumnIndex(ContactsContract.Contacts._ID)); 
    } 

    cursorID.close(); 

    Log.d(TAG, "Contact ID: " + contactID); 

    // Using the contact ID now we will get contact phone number 
    Cursor cursorPhone = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, 
      new String[]{ContactsContract.CommonDataKinds.Phone.NUMBER}, 

      ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ? AND " + 
        ContactsContract.CommonDataKinds.Phone.TYPE + " = " + 
        ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE, 

      new String[]{contactID}, 
      null); 

    if (cursorPhone.moveToFirst()) { 
     contactNumber = cursorPhone.getString(cursorPhone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); 
    } 

    cursorPhone.close(); 

    Log.d(TAG, "Contact Phone Number: " + contactNumber); 
    EditText callerNum = (EditText) findViewById(R.id.callerNum); 
    callerNum.setText(contactNumber); 
} 

private void retrieveContactName() { 

    String contactName = null; 

    // querying contact data store 
    Cursor cursor = getContentResolver().query(uriContact, null, null, null, null); 

    if (cursor.moveToFirst()) { 

     // DISPLAY_NAME = The display name for the contact. 
     // HAS_PHONE_NUMBER = An indicator of whether this contact has at least one phone number. 

     contactName = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)); 
    } 

    cursor.close(); 

    Log.d(TAG, "Contact Name: " + contactName); 
    EditText callerName = (EditText) findViewById(R.id.callerName); 
    callerName.setText(contactName); 
} 

所以,我的問題是如何知道聯繫人是否具有顯示圖片或沒有,所以我可以把默認的圖像,如果沒有圖像?謝謝。

這就是當我選擇一個名爲Act寬帶的聯繫人沒有照片或號碼時發生的事情;

的logcat:

06-20 22:48:32.267 31991-31991/fake.call.sms.fakecallandsmspro D/MainActivity3﹕ Contact Name: Act Broadband 
06-20 22:48:32.275  551-749/? W/InputMethodManagerService﹕ Starting input on non-focused client [email protected] (uid=10157 pid=31991) 
06-20 22:48:32.316 31991-31991/fake.call.sms.fakecallandsmspro D/MainActivity3﹕ Contact ID: 2428 
06-20 22:48:32.332 31991-31991/fake.call.sms.fakecallandsmspro D/MainActivity3﹕ Contact Phone Number: null 
06-20 22:48:32.345 31991-31991/fake.call.sms.fakecallandsmspro D/AndroidRuntime﹕ Shutting down VM 
06-20 22:48:32.346 31991-31991/fake.call.sms.fakecallandsmspro E/AndroidRuntime﹕ FATAL EXCEPTION: main 
    Process: fake.call.sms.fakecallandsmspro, PID: 31991 
    java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { dat=content://com.android.contacts/contacts/lookup/3789r3128-272B4D294943272D2927412D/2428 flg=0x1 }} to activity {fake.call.sms.fakecallandsmspro/fake.call.sms.fakecallandsmspro.MainActivity3}: java.lang.NullPointerException: Attempt to invoke virtual method 'void java.io.InputStream.close()' on a null object reference 
      at android.app.ActivityThread.deliverResults(ActivityThread.java:3574) 
      at android.app.ActivityThread.handleSendResult(ActivityThread.java:3617) 
      at android.app.ActivityThread.access$1300(ActivityThread.java:151) 
      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1352) 
      at android.os.Handler.dispatchMessage(Handler.java:102) 
      at android.os.Looper.loop(Looper.java:135) 
      at android.app.ActivityThread.main(ActivityThread.java:5254) 
      at java.lang.reflect.Method.invoke(Native Method) 
      at java.lang.reflect.Method.invoke(Method.java:372) 
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) 
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 
    Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void java.io.InputStream.close()' on a null object reference 
      at fake.call.sms.fakecallandsmspro.MainActivity3.retrieveContactPhoto(MainActivity3.java:124) 
      at fake.call.sms.fakecallandsmspro.MainActivity3.onActivityResult(MainActivity3.java:74) 
      at android.app.Activity.dispatchActivityResult(Activity.java:6192) 
      at android.app.ActivityThread.deliverResults(ActivityThread.java:3570) 
      at android.app.ActivityThread.handleSendResult(ActivityThread.java:3617) 
      at android.app.ActivityThread.access$1300(ActivityThread.java:151) 
      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1352) 
      at android.os.Handler.dispatchMessage(Handler.java:102) 
      at android.os.Looper.loop(Looper.java:135) 
      at android.app.ActivityThread.main(ActivityThread.java:5254) 
      at java.lang.reflect.Method.invoke(Native Method) 
      at java.lang.reflect.Method.invoke(Method.java:372) 
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) 
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 
06-20 22:48:32.348 551-12709/? W/ActivityManager﹕ Force finishing activity 1 fake.call.sms.fakecallandsmspro/.MainActivity3 
06-20 22:48:32.355 551-32557/? W/DropBoxManagerService﹕ Dropping: data_app_crash (1783 > 0 bytes)​ 
+0

Stacktrace? logcat的? – M4rtini

+0

Logcat:http://txs.io/tB5b @ M4rtini –

+1

NPE在124行(MainActivity3.java:124)。所以你知道是否有顯示圖片或沒有在那裏檢查空。 – M4rtini

回答

1

我會改變:

 if (inputStream != null) { 
      photo = BitmapFactory.decodeStream(inputStream); 
      // ImageView imageView = (ImageView) findViewById(R.id.callerPic); 
      imageView.setImageBitmap(photo); 
     } 
     if (inputStream == null) { 
      Toast.makeText(getApplicationContext(), "No profile pic found", Toast.LENGTH_LONG).show(); 
     } 

     assert inputStream != null; 
     inputStream.close(); 

 if (inputStream != null) { 
      photo = BitmapFactory.decodeStream(inputStream); 
      // ImageView imageView = (ImageView)findViewById(R.id.callerPic); 

      imageView.setImageBitmap(photo); 
      inputStream.close(); 
     }else{ 
      Toast.makeText(getApplicationContext(), "No profile pic found", Toast.LENGTH_LONG).show(); 
     } 

這種方式只在inputStream不爲null的情況下調用inputStream.close(),而不進行斷言。也沒有必要有if(inputStream != null)後跟if(inputStream == null)。所以我用'else'語句替換了第二個'if'語句。

讓我知道這是否有幫助!