Android Dagger 2 Dependency not being injected

好久不见. 提交于 2019-12-22 08:53:44

问题


i'm trying to use Dagger 2 into my apps but i'm having some problems regarding the entities repository and i haven't figured it out what i'm missing.

Here is my Application Component:

@Singleton
@Component(
        modules = {
                AndroidModule.class,
                RepositoryModule.class
        }
)

public interface ApplicationComponent {
    void inject(AndroidApplication app);
    IDependenceyRepository dependencyRepository();
}

My modules:

@Module
public class RepositoryModule {
    @Provides @Singleton IDependenceyRepository provideDependendencyRepository(Context context) {
        return new DependencyRepository(context);
    }
}

@Module
public class AndroidModule {
    private final AndroidApplication app;

    public AndroidModule(AndroidApplication app) {
        this.app = app;
    }

    @Provides @Singleton Context provideApplicationContext() {
        return app;
    }
}

My AndroidApplication:

public class AndroidApplication extends Application {
    private ApplicationComponent component;

    @Override
    public void onCreate() {
        super.onCreate();
        setupGraph();
    }

    private void setupGraph() {
        component = DaggerApplicationComponent.builder()
                .androidModule(new AndroidModule(this))
                .repositoryModule(new RepositoryModule())
                .build();
        component.inject(this);
    }

    public ApplicationComponent component() {
        return component;
    }
}

And here is my activity:

public class MainActivity extends Activity {
    @Inject
    IDependenceyRepository repository;

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

    private void testRepository() {
        Dependency dep = new Dependency();
        dep.setDescription("XXX");
        dep.setName("AAAA");

        try {
            repository.save(dep);
            Log.d("", repository.queryAll().get(0).getName());
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

What happens is that i'm getting a null pointer exception into the repository. He is not being injected.

If i add this line though:

repository = ((AndroidApplication) getApplication()).component().dependencyRepository();

It works, but the point of the DI it's not to have to worry about this, or am im wrong about that?

I've tried some other example and tutorials but haven't managed to solve my problem.

Thanks in advance,


回答1:


david.mihola's comment is correct: in order to be able to have @Inject fields initialized, you need to have a method (typically void inject(MyClass)) in your component.

It's worth noting (not specifically for your question, but it could come up) that if you have a base class:

public class BaseActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ((AndroidApplication) getApplication()).component().inject(this);
    }
} 

and in your component:

void inject(BaseActivity);

Injecting into a subclass that has it's own @Inject fields won't work:

public class ActualActivity extends BaseActivity {
    @Inject IDependenceyRepository repo;
}

This is because Dagger needs to know the concrete classes that will be injected at compile time, not runtime (like Dagger 1 could do).



来源:https://stackoverflow.com/questions/31372901/android-dagger-2-dependency-not-being-injected

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!