Attempt to invoke virtual method 'int java.lang.Integer.intValue()' on a null object reference


問題原因:

1,int和Integer判斷是否相等時出錯

2,用到int的地方,實際傳值是Integer

(int是基本類型,存數值;integer是對象,用一個引用指向這個對象)

解決方案:

對Integer參數進行null判斷,如果不為null,再將Integer值轉換成int

public Integer getResource(){
    return rid;
}

public void showImage(Integer resourceId){
    if(resourceId != null){
        Glide.with(context).load(resourceId.intValue());
    }
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM