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
  • 179,855
  • 19
  • 132
  • 245

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,764
  • 6
  • 35
  • 51
Sagar
  • 23,903
  • 4
  • 62
  • 62
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
  • 179,855
  • 19
  • 132
  • 245
AA Shakil
  • 538
  • 4
  • 14