revolutionary
revolutionary

Reputation: 3354

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

Upvotes: 5

Views: 2898

Answers (1)

Emmanuel
Emmanuel

Reputation: 13223

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

Upvotes: 3

Related Questions