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);
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);
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.