dagger 2-@subcomponent.builder缺少所需模块或子组件的设置程序

axkjgtzd  于 2021-06-30  发布在  Java
关注(0)|答案(0)|浏览(257)

我有两个望远镜,一个是 ApplicationScope 第二个是 ActivityScope . 我有两个组件。这个 ActivityComponentSubComponent 我的 ApplicationComponent . 我有一节课。当我注射的时候 @Inject 注解,它可以正常工作,但是当我创建一个模块并试图提供它时,我得到了一个错误- @Subcomponent.Builder is missing setters for required modules or subcomponents. 下面我留下密码。请帮我理解这个问题。

@ActivityLevelScope
public class Car {

private static final String TAG = "Car";
private Driver driver;
private CarSpecs carSpecs;

@Inject
public Car(Driver driver, CarSpecs carSpecs) {
    this.driver = driver;
    this.carSpecs = carSpecs;
}

public void drive() {
    Log.d(TAG, driver + " drives " + this);
    carSpecs.showSpecs();
}
}

//驾驶员等级

public class Driver {
}

//carspecs类

public class CarSpecs {

private String carName;
private int carYear;

public CarSpecs(@Named("car_name") String carName, @Named("car_year") int carYear) {
    this.carName = carName;
    this.carYear = carYear;
}

public void showSpecs() {
    Log.d(LOG_TAG, "Car is " + carName + " " + carYear);
}
}

好的,这是我的应用程序模型)下面是我的模块。它们是2:

@Module
public class CarSpecsModule {

private String carName;
private int carYear;

public CarSpecsModule(String carName, int carYear) {
    this.carName = carName;
    this.carYear = carYear;
}

@Provides
public String provideCarName() {
    return carName;
}

@Provides
public int provideCarYear() {
    return carYear;
}

@ActivityLevelScope
@Provides
public CarSpecs provideCarSpecs() {
    return new CarSpecs(carName, carYear);
}
}

//驱动模块

@Module
public abstract class DriverModule {

@Provides
@ApplicationLevelScope
static Driver provideDriver() {
    return new Driver();
}
}

好的,现在我将向您展示组件和活动。
这是我的applicationlevel组件。

@ApplicationLevelScope
@Component(modules = DriverModule.class)
public interface AppComponent {
    ActivityComponent.Builder getActivityComponentBuilder();
}

我还有一个应用程序类

public class App extends Application {

private AppComponent appComponent;

@Override
public void onCreate() {
    super.onCreate();
    appComponent = DaggerAppComponent.create();
}

public AppComponent getAppComponent() {
    return appComponent;
}
}

这是我的活动组件,所有的事情和错误都发生在这里)

@ActivityLevelScope
@Subcomponent(modules = CarSpecsModule.class)
public interface ActivityComponent {

Car getCar();

@Subcomponent.Builder
interface Builder {

    @BindsInstance
    Builder carName(@Named("car_name") String carName);

    @BindsInstance
    Builder carYear(@Named("car_year") int carYear);

    ActivityComponent build();
}
}

如你所见,我有一个 subComponent 在这里。下面是 mainActivity :

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ActivityComponent component = ((App) getApplication()).getAppComponent()
            .getActivityComponentBuilder()
            .carName("Audi")
            .carYear(2008)
            .build();

    Car injectedCar1 = component.getCar();
    Car injectedCar2 = component.getCar();
    injectedCar1.drive();
    injectedCar2.drive();
}

注意:当我使用@injectannotation注入carspecs时,一切正常。但是当我试图提供它时,我得到了一个错误 @Subcomponent.Builder is missing setters for required modules or subcomponents 但为什么呢?
p、 看来我已经解决了我的问题。在我的子组件中,我为carspecs字段创建了一个生成器,但没有为carspecs本身创建。所以首先我把这个生成器改成了工厂,以获得更简洁的代码。我在那家工厂提供了汽车规格,而不是田地。
所以工作准则是:

@ActivityLevelScope
@Subcomponent(modules = CarSpecsModule.class)
public interface ActivityComponent {

Car getCar();

@Subcomponent.Factory
interface Factory {
    ActivityComponent create(CarSpecsModule carSpecsModule);
}
}

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题