Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 808
Rob Avatar asked Sep 10 '25 18:09

Rob


1 Answers

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

like image 133
Goku Avatar answered Sep 13 '25 08:09

Goku