本文介绍了Dagger 2.10 / 2.11注入活动失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直试图将Activity注入ViewUtils类中,但未成功。我关注了几个不同的帖子,但是我似乎无法理解我在实现过程中缺少的内容。

I have been trying to, unsuccessfully, inject the Activity in a class ViewUtils. I have followed a couple of different posts but I can't seem to understand what am I missing in my implementation.

我知道这可能是下面这些帖子的重复我为此深表歉意,但老实说,我看不到我在想什么。这些是我发现的帖子:

I know this is probably a repetition of the posts below and I really apologize for that but I honestly cannot see what am I missing. These are the posts I've found:





  • Dagger 2.10 Android subcomponents and builders
  • How to create custom scoped modules in dagger 2.10
  • https://google.github.io/dagger/subcomponents.html

我的实现如下:

AppComponent

@Component(modules = {
        AppModule.class, AndroidSupportInjectionModule.class, ActivityBindingModule.class
}) @Singleton public interface AppComponent extends AndroidInjector<EmblyApp> {

    @Component.Builder abstract class Builder extends AndroidInjector.Builder<EmblyApp> {}
}

ActivityBindingModule

@Module public abstract class ActivityBindingModule {

    @ContributesAndroidInjector
    abstract LoginActivity loginActivity();
}

LoginSubcomponent

@Subcomponent(modules = LoginSubcomponent.LoginActivityModule.class)
public interface LoginSubcomponent extends AndroidInjector<LoginActivity> {

    @Subcomponent.Builder abstract class Builder extends AndroidInjector.Builder<LoginActivity> {}

    @Module abstract class LoginActivityModule {

        @Binds abstract Activity bindActivity(LoginActivity activity);

        @Provides @ActivityScope static ViewUtils viewUtils(Activity activity) {
            return new ViewUtils(activity);
        }
    }
}

ViewUtils

public class ViewUtils {

    private final Activity activity;

    @Inject public ViewUtils(Activity activity) {
        this.activity = activity;
    }
}

我得到的错误是:

Error:(14, 22) error: [dagger.android.AndroidInjector.inject(T)] android.app.Activity cannot be provided without an @Inject constructor or from an @Provides-annotated method.
android.app.Activity is injected at
com.emblyapp.app.ui.helpers.ViewUtils.<init>(activity)
com.emblyapp.app.ui.helpers.ViewUtils is injected at
com.emblyapp.app.ui.authentication.login.LoginActivity.viewUtils
com.emblyapp.app.ui.authentication.login.LoginActivity is injected at
dagger.android.AndroidInjector.inject(arg0)

这是什么问题?谢谢您的帮助!

What is wrong in here? Thanks for the help!

编辑:我忘了说我的LoginActivity带有AndroidInjection注入了。

I forgot to mention my LoginActivity has the injection with the AndroidInjection

@Override protected void onCreate(Bundle savedInstanceState) {
        AndroidInjection.inject(this);
        super.onCreate(savedInstanceState);
    }


推荐答案

在:

因此,我们可以摆脱 LoginSubcomponent 并在 ActivityBindingModule 中执行以下更改:

Thus, we can get rid of LoginSubcomponent and perform following changes in ActivityBindingModule:




    @Module
    public abstract class ActivityBindingModule {

        @ActivityScope
        @ContributesAndroidInjector(modules = LoginActivityModule.class)
        abstract LoginActivity loginActivity();
    }


LoginActivityModule.java




    @Module
    abstract class LoginActivityModule {

        @Binds
        abstract Activity bindActivity(LoginActivity activity);

        @Provides
        @ActivityScope
        static ViewUtils viewUtils(Activity activity) {
            return new ViewUtils(activity);
        }
    }


您的自定义应用程序类:

Your custom application class:




    public class MyApp extends DaggerApplication {

        @Inject
        DispatchingAndroidInjector dispatchingActivityInjector;

        @Override
        protected AndroidInjector applicationInjector() {
            return DaggerAppComponent.builder().create(this);
        }

    }


这篇关于Dagger 2.10 / 2.11注入活动失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-13 23:24