java.lang.ClassCastException: android.app.Application cannot be cast to… MyApplication

Resolutin of java.lang.ClassCastException: android.app.Application cannot be cast to… MyApplication.

While running an android project at the time i was integrating dagger , i got an error that display this on my log :

java.lang.ClassCastException error: android.app.Application cannot be cast to… MyApplication.

I still get these kind of errors even if i am used to use the android dependency injection framework, Dagger.

it came very often and, often i am still forgetting the solution <lol>.

I believe that it can happen to a lot of android developpers who are using Dagger on their projects. The reason i forget the solution all the time is because it’s so simple to solve it.

Let find it out on the next sections !!

Cause of java.lang.ClassCastException dagger problem: android.app.Application cannot be cast to…

You are implementing an android project with Dagger.

I use a custom class that inherits from the class Application (I called; MyApplication ) to inject dependancy in the android framework to loose coupling our code.

When running your project the error java.lang.ClassCastException: android.app.Application cannot be cast to… MyApplication  is shown.

The program failled to convert the Application class into my custom Application class ( called MyApplication ).

It happen because… well, i forgot to register my custom application class inside the manifest file.

Now let see how pratically we can solve it !!

Solution

To solve the problem we must attack Manifest file of your android project.

You must add in the tag attribute android: name to register your Custom Application class like the code below.

<application
    ...
    android: name = ". mypackage.MyApplication"
    ...> ...

That’s it that’s all.

It’s that simple.

Next time you forget it (including me) don’t hesitate to just type the error in the google search or my troubleshooter search !!

java.lang.ClassCastException: android.app.Application cannot be cast to… MyApplication

Leave a Comment