0

Im trying to add some dependency injection in my android project, so following example what i found i created my module

@Module(entryPoints = {MyActivity.class})

public class MyModule {

    private final Context context;

    public MyModule (Context context) {
        this.context = context.getApplicationContext();
    }

    @Provides
    @Singleton
    Context applicationContext() {
        return context;
    }

    @Provides
    @Singleton
    EventBus provideEventBus() {
        return EventBusFactory.create();
    }
}

i have my application:

public class MyApplication extends Application{

    private ObjectGraph objectGraph;

    @Override
    public void onCreate() {
        super.onCreate();
        objectGraph = ObjectGraph.get(new MyModule(this));
    }

    public ObjectGraph getObjectGraph() {
        return objectGraph;
    }
}

and activity

public class MyActivity extends Activity {

    @App
    MyApplication application;

    @Inject
    EventBus eventBus;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ObjectGraph objectGraph = application.getObjectGraph();
        objectGraph.inject(this);
    ....

i'm not sure what i'm doing wrong or what i missed, but my eclipse shows me error:

The method get(Class) in the type ObjectGraph is not applicable for the arguments (MyModuleModule)

in this line objectGraph = ObjectGraph.get(new MyModule(this));

and when i'm trying to do build i get:

No binding for my.android.lib.EventBus required by my.android.app.MyActivity for my.android.app.MyModule

is anyone can tell me what i'm doing wrong and what i forgot about?

user902383
  • 8,420
  • 8
  • 43
  • 63

1 Answers1

2

The instance of ObjectGraph class can be obtained by invoking the create() static method.

Try

objectGraph = ObjectGraph.create(new MyModule(this));

For the binding error, you have to add the EventBus library into your build classpath.

Jiyong Park
  • 664
  • 3
  • 6
  • i expect to maven add my lib jar to my classpath after i added to my pom file, shall i do sth extra then? – user902383 Feb 14 '13 at 14:43
  • Well, I don't know muchabout Maven, but the error apparently says that EventBus library is not in yout build class path. – Jiyong Park Feb 14 '13 at 15:00
  • you were right, i didn't notice and i used wrong class, but first part of my question is still not solved, after i follow your suggestion, i got "Cannot make a static reference to the non-static method" error – user902383 Feb 14 '13 at 16:21
  • Indeed. In the earliest days the static method ObjectGraph.create(SomeModule) used to be called .get(). We renamed it to create() because we wanted to use myGraph.get(SomeClass.class) as the means by which you got an instance FROM the object graph, not create the graph itself. – Christian Gruber Feb 14 '13 at 16:47