Search code examples
javaandroidnosuchmethoderror

NoSuchMethodError on certain OS versions


I’m not sure why I’m getting this error only on certain Android versions (below 5.0).

I’m calling:

myImageView.setImageDrawable(getResources().getDrawable(R.drawable.image, null));

Because of this I’m getting a NoSuchMethodError.
What to do?


Solution

  • Use this

    myImageView.setImageDrawable(ContextCompat.getDrawable(this,R.drawable.image));
    

    Instead of this

    myImageView.setImageDrawable(getResources().getDrawable(R.drawable.image, null));
    

    EDIT

    when you use setImageDrawable(getResources().getDrawable(R.drawable.image, null)); this it will shows below error

    enter image description here

    NOTE setImageDrawable(getResources().getDrawable(R.drawable.image, null)); this method is added in API level 21

    You can read more about ContextCompat