I'm trying to get the Dagger simple example running in IntelliJ, but it's failing on the getApplication call in the DemoBaseActivity class:
public abstract class DemoBaseActivity extends Activity {
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
((DemoApplication) getApplication()).inject(this);
}
}
When it hits the injection method, the app fails silently with no errors. I've built the project with an instance of the base class Activity instead of the DemoBaseActivity and it deploys just fine.
Any thoughts?
EDIT:
It seem sto be breaking when the return value of the Activity's getApplication() call is cast as custom DemoApplication type.
package com.badlogic.androidgames.simple;
import android.app.Application;
import dagger.ObjectGraph;
import java.util.Arrays;
import java.util.List;
public class DemoApplication extends Application
{
private ObjectGraph graph;
@Override
public void onCreate()
{
super.onCreate();
graph = ObjectGraph.create(getModules().toArray());
}
protected List<Object> getModules()
{
return Arrays.asList(new AndroidModule(this), new DemoModule());
}
public void inject(Object object)
{
graph.inject(object);
}
}
EDIT: Here's the stack trace:
10-03 05:15:25.331: ERROR/AndroidRuntime(5035): FATAL EXCEPTION: main java.lang.RuntimeException: Unable to start activity ComponentInfo{com.badlogic.androidgames.simple/com.badlogic.androidgames.simple.ui.HomeActivity}: java.lang.ClassCastException: android.app.Application cannot be cast to com.badlogic.androidgames.simple.DemoApplication at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261) at android.app.ActivityThread.access$600(ActivityThread.java:141) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:137) at android.app.ActivityThread.main(ActivityThread.java:5103) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:525) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) at dalvik.system.NativeStart.main(Native Method) Caused by: java.lang.ClassCastException: android.app.Application cannot be cast to com.badlogic.androidgames.simple.DemoApplication at com.badlogic.androidgames.simple.DemoBaseActivity.onCreate(DemoBaseActivity.java:33) at com.badlogic.androidgames.simple.ui.HomeActivity.onCreate(HomeActivity.java:30) at android.app.Activity.performCreate(Activity.java:5133) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
As discussed in comments - you forgot to mention your application class in AndroidManifest file. And that was the fix:
<application
android:name="com.badlogic.androidgames.simple.DemoApplication"
...
>
...
</application>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With