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

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
查阅资料可以得到这个解释:

改成这样即可

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.

猜你喜欢

转载自blog.csdn.net/qq_25749749/article/details/81288190