RoboGuice – Pitfall – RuntimeException on Activities

Last weekend, I started developing a application to sync OpenStreetMap contacts into my Android devices.

Wanting to avoid boilerplate code, and to experiment a little with Dependency Injection in Android, i based my work on RoboGuice. I think I followed all the example code, but my activities would simply crash with a RuntimeException on startup.

Since finding the root cause of this problem was not as trivial as I wished it to be, I decided to write this up as an article for myself and of course for all the other Android developers out there who don’t want to waste time hunting this issue when they could be gold-plating their apps 🙂

To cut to the core of the problem: If your activity is throwing this:


E/AndroidRuntime( 246): Uncaught handler: thread main exiting due to uncaught exception
E/AndroidRuntime( 246): java.lang.RuntimeException: Unable to start activity
ComponentInfo{com.gurkensalat.osm.osmsync/com.gurkensalat.osm.osmsync.HelloAndroidActivity}: java.lang.ClassCastException: android.app.Application
E/AndroidRuntime( 246): at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496)
...

maybe you need to declare your actual application class type in AndroidManifest.xml:


<application android:icon="@drawable/icon"
android:label="@string/app_name"
android:name="roboguice.application.RoboApplication">
...
</application>