使用ContributesAndroidInjector提供活动

我在为Activity需要此对象的依赖对象提供对象时遇到问题。


我使用Dagger 2.13进行了非常标准的设置,如下所示:


AppComponent.java


@Singleton

@Component(modules = {

        AndroidInjectionModule.class,

        AppModule.class,

        ActivityModule.class

})

public interface AppComponent extends AndroidInjector<Appname> {

    @Component.Builder

    interface Builder {

        @BindsInstance

        Builder application(Appname appname);

        AppComponent build();

    }


    void inject(Appname appname);

}

ActivityModule.java


@Module

abstract public class ActivityModule {

    @ContributesAndroidInjector(modules = {MainActivityModule.class, MainActivityFragmentsModule.class})

    abstract MainActivity contributeMainActivity();

}

MainActivityModule.java


@Module

public class MainActivityModule {


@Provides

@Singleton

static Billing provideBilling(Context context) {

    return new Billing(context);

}


@Provides

@Singleton

static ActivityCheckout provideActivityCheckout(MainActivity activity, Billing billing) {

    return ActivityCheckout.forActivity(activity, billing);

}

}

MainActivityFragmentsModule.java


@Module

abstract public class MainActivityFragmentsModule {

    @ContributesAndroidInjector

    abstract WelcomeFragment contributeWelcomeFragment();

}

当我尝试使用时ActivityCheckout,WelcomeFragment出现错误,无法提供此依赖关系:


Error:(20, 8) error: [dagger.android.AndroidInjector.inject(T)] org.solovyev.android.checkout.ActivityCheckout cannot be provided without an @Inject constructor or from an @Provides-annotated method.


似乎未提供该活动,但我不知道为什么。我试图遵循其中一个相同设置的教程之一,并且有可能注入Activity对象。


我使用DaggerApplication,DaggerAppCompatActivity和DaggerFragment。


ABOUTYOU
浏览 289回答 1
1回答

临摹微笑

尽管我不确定如何/为什么会显示您所显示的错误,但是您在@Singleton范围内错误地注册了活动范围的内容。请注意,您发布的错误消息抱怨没有提供ActivityCheckout。如果无法提供您的活动,则可能会找到有关缺少MainActivity的错误消息。我的直觉是,您的编译中存在多个错误,但您仅发布了最后一个错误,而较早的错误表明您无法将@Singleton绑定安装到@ContributesAndroidInjector默认创建的不受作用域的子组件中。因此,Dagger会忽略该@Provides方法,并且会收到错误消息。@Provides@Singleton&nbsp; // BAD: If this is Singleton, it will outlive and leak MainActivity.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Dagger will complain about mismatched scopes, but it's right:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // It doesn't make sense for ActivityCheckout to be @Singleton.static ActivityCheckout provideActivityCheckout(&nbsp; &nbsp; MainActivity activity, Billing billing) {&nbsp; return ActivityCheckout.forActivity(activity, billing);}而是创建一个特定于活动的范围,该范围指示每个活动都有自己的活动。@Retention(RetentionPolicy.RUNTIME)&nbsp; &nbsp; // Not used at runtime, but JSR-330@Scope&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// requires that @Scopes are kept at RUNTIME.public @interface ActivityScope {}&nbsp; &nbsp; &nbsp;// PerActivity is also a good name.现在,用它标记@ContributesAndroidInjector,以便您生成的子组件具有该作用域:@ContributesAndroidInjector(&nbsp; &nbsp; modules = {MainActivityModule.class, MainActivityFragmentsModule.class})@ActivityScopeabstract MainActivity contributeMainActivity();而且您的绑定也是如此,因此它们与您的活动组件的生命周期相匹配:@Module public class MainActivityModule {&nbsp; // I'm assuming this is actually activity scoped, but if it's truly singleton,&nbsp; // leave it @Singleton and move it to AppModule.&nbsp; @Provides&nbsp; @ActivityScope&nbsp; static Billing provideBilling(Context context) {&nbsp; &nbsp; return new Billing(context);&nbsp; }&nbsp; @Provides&nbsp; @ActivityScope&nbsp; static ActivityCheckout provideActivityCheckout(&nbsp; &nbsp; &nbsp; MainActivity activity, Billing billing) {&nbsp; &nbsp; return ActivityCheckout.forActivity(activity, billing);&nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java