[英]“No inject registered for a class” even though I declare it
I'm new to Dagger and am trying to figure out why a particular dependency is not being injected. 我是Dagger的新手,正在尝试弄清楚为什么没有注入特定的依赖项。 I'm getting the following error when attempting to inject a Presenter in a Fragment:
尝试将Presenter插入片段时出现以下错误:
Caused by: java.lang.IllegalArgumentException: No inject registered for members/com.company.myapp.view.fragment.OverviewFragment. You must explicitly add it to the 'injects' option in one of your modules.
at dagger.ObjectGraph$DaggerObjectGraph.getInjectableTypeBinding(ObjectGraph.java:302)
at dagger.ObjectGraph$DaggerObjectGraph.inject(ObjectGraph.java:279)
at com.company.myapp.view.fragment.OverviewFragment.initializePresenter(OverviewFragment.java:50)
at com.company.myapp.view.fragment.BaseFragment.onViewCreated(BaseFragment.java:28)
at com.company.myapp.view.fragment.OverviewFragment.onViewCreated(OverviewFragment.java:84)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:871)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1040)
at android.app.FragmentManagerImpl.addFragment(FragmentManager.java:1142)
at android.app.Activity.onCreateView(Activity.java:4786)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:689)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:290)
at android.app.Activity.setContentView(Activity.java:1929)
at com.company.myapp.view.activity.HomeActivity.onCreate(HomeActivity.java:23)
at android.app.Activity.performCreate(Activity.java:5231)
... ...
I have an ApplicationModule which pulls in the necessary app-wide modules (such as analytics). 我有一个ApplicationModule,它引入了必要的应用程序级模块(例如分析)。
@Module(
injects = {
BaseApplication.class,
MyApplication.class,
TestMyApplication.class
},
includes = { DomainModule.class }
)
public class MyModule {
private final BaseApplication mApp;
public MyModule(BaseApplication app) {
mApp = app;
}
@Provides
@Singleton
public Context provideApplicationContext() {
return mApp;
}
}
I have an OverviewModel which directly correlates to an OverviewFragment. 我有一个OverviewModel直接与OverviewFragment相关。 The whole purpose of this module is to inject the Presenter into the OverviewFragment.
该模块的全部目的是将Presenter注入OverviewFragment。
@Module(
injects = OverviewFragment.class,
addsTo = ReviewsModule.class
)
public class OverviewModule {
private OverviewView mView;
public OverviewModule(OverviewView view) {
mView = view;
}
@Provides
@Singleton
public OverviewView provideView() {
return mView;
}
@Provides
@Singleton
public OverviewPresenter provideOverviewPresenter(OverviewView view) {
return new OverviewPresenter(view);
}
}
The object graph is created in the onCreate of a BaseApplication object (with the Reviews module) and injected ( Modules.list()
returns a list of modules, currently a blank TestReviewsModule and a ReviewsModule... seen above). 对象图在BaseApplication对象的onCreate中创建(带有Reviews模块)并注入(
Modules.list()
返回模块列表,当前为空白的TestReviewsModule和ReviewsModule ...如上所示)。
private void initializeObjectGraph() {
Timber.d("Initializing application object graph!");
og = ObjectGraph.create(Modules.list(this));
og.inject(this);
}
In the OverviewFragment, I retrieve this created object graph a(onViewCreated) and do the following to add my OverviewModule to the application wide object graph: 在OverviewFragment中,我检索此创建的对象图a(onViewCreated),然后执行以下操作以将OverviewModule添加到应用程序范围的对象图中:
public class OverviewFragment extends BaseFragment implements OverviewView {
protected ObjectGraph og;
@Inject OverviewPresenter mPresenter;
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
og = BaseApplication.getInstance().getAppObjectGraph();
initializePresenter();
}
@Override
void initializePresenter() {
og.plus(new OverviewModule(this));
og.inject(this);
mPresenter.initialize(mVendorId);
}
}
Why is my application saying I haven't registered even though I explicitly state that I am injecting into OverviewFragment in my OverviewModule? 为什么我的应用程序说即使我明确声明要注入OverviewModule中的OverviewFragment,也没有注册?
Dagger/DI troubleshooting is kind of difficult, so I'll be as thorough as possible... maybe this will help some others out. Dagger / DI故障排除有点困难,所以我会尽可能的彻底...可能会帮助其他人。
I found the problem after reading some other SO questions (such as Dagger can't find injectable members on a module ) and some documentation. 在阅读了其他一些SO问题(例如Dagger在模块上找不到可注入的成员 )和一些文档之后,我发现了问题。
My foundational problem was a lack of understanding of the .plus()
method. 我的基本问题是对
.plus()
方法的了解不足。 I didn't realize that it returns a NEW ObjectGraph instead of modifying the graph it was called on. 我没有意识到它返回一个新的ObjectGraph而不是修改它被调用的图。 By doing a
.plus()
and then injecting on the same ObjectGraph I called the .plus()
on, I was essentially attempting to inject from my ReviewsModule... which has no knowledge of OverviewFragment and hence the error. 通过执行一个
.plus()
,然后在我调用了.plus()
的同一ObjectGraph上进行注入,我实质上是试图从我的ReviewsModule中注入…,而它不了解OverviewFragment,因此不知道该错误。
In the OverviewFragment I needed to make the change from: 在OverviewFragment中,我需要从以下位置进行更改:
// old way
void initializePresenter() {
og.plus(new OverviewModule(this)); // Note this line
og.inject(this);
mPresenter.initialize(mVendorId);
}
to 至
// new way
void initializePresenter() {
og = og.plus(new OverviewModule(this)); // Note this line
og.inject(this);
mPresenter.initialize(mVendorId);
}
This returns a new graph with both OverviewModule and ReviewsModule. 这将返回带有OverviewModule和ReviewsModule的新图。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.