Reputation: 6657
I'm using dagger for dependency injection and from time to time I get this class cast exception:
Caused by: java.lang.ClassCastException: android.app.Application cannot be
cast to myPackage.MyApplication
at myPackage.ui.activity.BaseActivity.injectAppComponent(BaseActivity.java:63)
at myPackage.ui.activity.BaseActivity.onCreate(BaseActivity.java:42)
Here's my Base Activity method where I do the injection:
protected void injectAppComponent() {
((MyApplication) getApplication()).getAppComponent().inject(this);
}
Application Class
public class MyApplication extends Application {
private AppComponent appComponent;
private void injectComponent() {
appComponent = DaggerAppComponent.builder()
.appModule(new AppModule(this)).build();
appComponent.inject(this);
}
public AppComponent getAppComponent() {
return appComponent;
}
}
In my manifest, i define the app name like this:
application
android:name="myPackage.MyApplication"
But I still experience this issue, any thoughts why this is happening?
Upvotes: 4
Views: 2789
Reputation: 31
You probably aren't still looking for an answer, but I just ran into this issue and my problem was that the interface of the class that I was trying to inject was package-private.
I changed it to public and everything worked.
Upvotes: 3