java.lang.NoSuchMethodError:android.content.Context.getDrawable

今天在開發的時候,這個代碼在源碼中是能夠看到的,可是在android 4.3手機上面會報錯,具體錯誤信息和代碼以下:
setBackgroundDrawable(context.getDrawable(R.drawable.coach_popou_window));
會報這個錯誤
java.lang.NoSuchMethodError:android.content.Context.getDrawable
查閱資料能夠獲得這個解釋:

 

改爲這樣便可java

setBackgroundDrawable(ContextCompat.getDrawable(context,R.drawable.coach_popou_window))主要緣由是由於是版本不對,若是是android 5.0以上的手機是支持的Prior to android.os.Build.VERSION_CODES#JELLY_BEAN, this function would not correctly retrieve the final configuration density when the resource ID passed here is an alias to another Drawable resource. This means that if the density configuration of the alias resource is different than the actual resource, the density of the returned Drawable would be incorrect, resulting in bad scaling.
相關文章
相關標籤/搜索
本站公眾號
   歡迎關注本站公眾號,獲取更多信息