dagger简单示例给出了IllegalStateException

时间:2014-04-22 09:55:04

标签: android dagger

我正在试验Dagger。现在我还没有完全理解一切是如何运作的。 所以我写了一个测试项目。

这是我的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);
    }
}

这是我的ActivityModule:

@Module(
        injects =
                {
                        Vehicle.class,
                        MainActivity.class,
                        Wheels.class
                }
)
public class ActivityModule extends Application{

    @Provides Wheels provideWheels()
    {
        return new Wheels(4,"X12");
    }
}

在我的清单中,我在应用程序中添加了ActivityModule作为名称。

这是我的车辆类:

@Module(
        includes = Wheels.class
)
public class Vehicle {
    @Inject
    Wheels wheels;
    private String type;

    public Vehicle(String type) {
        this.type = type;
    }
}

这是我的车轮:

public class Wheels {
    private int inch;
    private String brand;

    @Inject
    public Wheels(int inch, String brand) {
        this.inch = inch;
        this.brand = brand;
    }
}

现在我想要完成的是我在MainActivity中有一辆车注入他的车轮。现在我不知道如何在mainActivity中创建我的汽车,因为我想在用户填写的内容上创建一个带有参数String的汽车。

我明白了:

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

2 个答案:

答案 0 :(得分:1)

我发现了问题。这是我编辑的代码。问题是图表的创建。

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;
    }
}

完成false是必需的,否则dagger会抱怨注入构造函数Integer和String。

答案 1 :(得分:1)

@Module(
        includes = Wheels.class
)
public class Vehicle {

实际上这导致了编译时错误,因为Wheels不是模块