Reputation: 8118
I'm experimenting with Dagger. Now I don't fully understand how everything works. So I wrote a test project.
This is my MainActivity:
public class MainActivity extends Activity {
private ObjectGraph mActivityGraph;
@Inject Vehicle car;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mActivityGraph = ObjectGraph.create(new ActivityModule());
mActivityGraph.validate();
mActivityGraph.inject(this);
}
}
This is my ActivityModule:
@Module(
injects =
{
Vehicle.class,
MainActivity.class,
Wheels.class
}
)
public class ActivityModule extends Application{
@Provides Wheels provideWheels()
{
return new Wheels(4,"X12");
}
}
In my manifest I added the ActivityModule as name at the application.
This is my Vehicle class:
@Module(
includes = Wheels.class
)
public class Vehicle {
@Inject
Wheels wheels;
private String type;
public Vehicle(String type) {
this.type = type;
}
}
and this is my wheels:
public class Wheels {
private int inch;
private String brand;
@Inject
public Wheels(int inch, String brand) {
this.inch = inch;
this.brand = brand;
}
}
Now what I want to accomplish is that I have a car in MainActivity that injects his wheels. Now I don't know how to create my car in the mainActivity because I want to create a car with a parameter as String on what the user fills in.
I get this:
Error:(8, 8) error: Graph validation failed: No @Module on edu.ida.daggertest.app.Wheels
Error:(20, 8) error: Unknown error java.lang.IllegalStateException thrown by javac in graph validation: Unable to create binding for edu.ida.daggertest.app.Vehicle
Upvotes: 2
Views: 2346
Reputation: 3334
@Module(
includes = Wheels.class
)
public class Vehicle {
Actually this caused your compile-time error, because Wheels
is not a module
Upvotes: 1
Reputation: 8118
I found the problem. This is my edited code. The problem was the creation off the graph.
public class MainActivity extends Activity {
final static String TAG = MainActivity.class.getName();
private ObjectGraph mActivityGraph;
@Inject
Vehicle car;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mActivityGraph = ObjectGraph.create(new ActivityModule());
mActivityGraph.inject(this);
Log.d(TAG,String.valueOf(car.getWheels().getInch()));
Log.d(TAG,car.getWheels().getBrand());
}
}
@Module(
injects =
{
Vehicle.class,
MainActivity.class,
Wheels.class
},
complete = false
)
public class ActivityModule{
@Provides Wheels provideWheels()
{
return new Wheels(4,"X12");
}
}
public class Vehicle {
@Inject
Wheels wheels;
private String type;
public String getType() {
return type;
}
public Wheels getWheels() {
return wheels;
}
}
public class Wheels {
private int inch;
private String brand;
@Inject
public Wheels(int inch, String brand) {
this.inch = inch;
this.brand = brand;
}
public int getInch() {
return inch;
}
public String getBrand() {
return brand;
}
}
The completes false is required otherwise dagger is going to complain about injectable constructors Integer and String.
Upvotes: 1