今天在開發的時候,這個代碼在源碼中是可以看到的,但是在android 4.3手機上面會報錯,具體錯誤信息和代碼如下:
setBackgroundDrawable(context.getDrawable(R.drawable.coach_popou_window));
會報這個錯誤
java.lang.NoSuchMethodError:android.content.Context.getDrawable
查閱資料可以得到這個解釋:
改成這樣即可
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.