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
  • 151,199
  • 17
  • 111
  • 216

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,466
  • 6
  • 33
  • 50
Sagar
  • 22,451
  • 4
  • 56
  • 59
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
  • 151,199
  • 17
  • 111
  • 216
AA Shakil
  • 518
  • 4
  • 14