Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom View unable to get Activity handle from View.getContext

I have a custom view inside an activity, which i am launching with an Application context.

When i call the View.getContext() from within my CustomView and try to cast it:

Activity activity = (Acitivity)View.getContext();

It gives an error that cannot cast the Context from Application to Activity.

Has someone faced this problem before ??? please help

like image 590
revolutionary Avatar asked Oct 22 '22 01:10

revolutionary


1 Answers

As Romain Guy says in this post, there is no guarantee that the Context used by your View is an Activity. Also, if you are using the Applcation Context you cannot cast it to the Activity Context; they are different. This link gives a great explanation of all the different flavors of Context

like image 88
Emmanuel Avatar answered Oct 30 '22 15:10

Emmanuel