Dagger 2不会注入我的对象,但可以从组件获得 [英] Dagger 2 does not inject my Object but can be obtained from component
问题描述
我有我的组件
@GithubListActivityScope
@Component(modules = { GithubListActivityModule.class,GlideActivityModule.class })
public interface GithubListActivityComponent {
GithubUserListAdapter githubUserListAdapter ( );
RequestManager requestManager();
LinearLayoutManager linearLayoutManager();
}
我有一个像这样的模块:
And I have a module like this :
@Module
public class GithubListActivityModule {
private final Activity githubListActivity;
public GithubListActivityModule ( Activity activity ) {
this.githubListActivity = activity;
}
@Provides
@GithubListActivityScope
Activity activity ( ) {
return this.githubListActivity;
}
@Provides
@GithubListActivityScope
public LinearLayoutManager linearLayoutManager(Activity activity){
return new LinearLayoutManager ( activity );
}
}
问题:
我有注入的恐惧感像这样的LinearLayout管理器:
Problem : I have treid to inject LinearLayout manager like this :
@GithubListActivityScope
@Inject
LinearLayoutManager linearLayoutManager;
虽然我的组件的构建方式如下:
While my Component is built like this :
githubListActivityComponent = DaggerGithubListActivityComponent.builder ()
.githubListActivityModule ( new GithubListActivityModule ( this ) )
.build ();
我的线性布局管理器未实例化。但是当我手动执行
my Linear Layout manager does not get instantiated. But when I manually do
linearLayoutManager = githubListActivityComponent.linearLayoutManager ();
工作正常。我在哪里错了?
It works fine. Where am I going wrong?
推荐答案
我通过的每个地方活动
,我应该完全传递 相同的 类名(而不是其父类),所以一旦编辑了每个参数并返回了活动
放入 GithubListActivity
,然后添加
Everywhere that I am passing Activity
, I should pass exactly the same class Name (not its parent) So once edited every parameters and return types that were "Activity"
into "GithubListActivity"
and then added
void inject( GithubListActivity activity);
在组件类
然后注入 GithubListActivity
就像这样:
then injected "GithubListActivity"
like this :
DaggerGithubListActivityComponent.builder ()
.githubListActivityModule ( new GithubListActivityModule ( this ) )
.build ().inject ( this );
然后代码对我有用..
Then the codes worked for me ..
课程:
1.定义注入方法并注入当前活动
2。使用完全相同类型的对象(而不是父对象),在这种情况下,请使用 GithubListActivity而不是仅使用活动。
这篇关于Dagger 2不会注入我的对象,但可以从组件获得的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!