0

I am trying to cast a drawable in my code but I get this error

An instance of type android.view.View' can not be of type android.graphics.drawable.Drawable

Here is my code:

mydrawable = (Drawable) findViewById(R.drawable.mydrawable);
OneCricketeer
  • 126,858
  • 14
  • 92
  • 185

2 Answers2

0

Return type of findViewById() is a View which cannot be casted to Drawable.

Replace

(Drawable) findViewById(R.drawable.mydrawable);

with

ContextCompat.getDrawable(getActivity(), R.drawable.mydrawable);
ColdFire
  • 6,116
  • 6
  • 32
  • 50
Sagar
  • 20,467
  • 4
  • 50
  • 56
0

You can't cast this View as Drawable. You can cast this as EditText, TextView because those finally extend the class View. But Drawable doesn't extend View.

Try this

mydrawable = ResourcesCompat.getDrawable(getResources(), R.drawable.mydrawable, null);

This will be type of android.graphics.drawable.Drawable and your problem may be solved.

OneCricketeer
  • 126,858
  • 14
  • 92
  • 185
AA Shakil
  • 478
  • 4
  • 14