android.app.Application cannot be cast to android.app.Activity
You are passing the Application Context
not the Activity Context
with
getApplicationContext();
Wherever you are passing it pass this
or ActivityName.this
instead.
Since you are trying to cast the Context
you pass (Application not Activity as you thought) to an Activity
with
(Activity)
you get this exception because you can't cast the Application to Activity
since Application is not a sub-class of Activity
.
in case your project use dagger, and then this error show up you can add this at android manifest
<application
...
android: name = ".BaseApplication"
...> ...
In my case, when I'm in an activity that extends from AppCompatActivity
, it did not work(Activity) getApplicationContext ()
, I just putthis
in its place.
You are getting this error because the parameter required is Activity and you are passing it the Application.
So, either you cast application to the Activity like: (Activity)getApplicationContext();
Or you can just type the Activity like: MyActivity.this