我是新來的匕首,並試圖找出爲什麼不注入一個特定的依賴關係。當試圖在一個片段中注入演示,我發現了以下錯誤:「即使我聲明它沒有註冊註冊類」
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)
...
我有拉動必要的應用級模塊的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;
}
}
我有一個OverviewModel直接關聯到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);
}
}
對象圖是在BaseApplication對象的OnCreate(與評價模塊)中創建並注射(Modules.list()
返回模塊的列表,目前爲空白TestReviewsModule和ReviewsModule ...上方觀察)。
private void initializeObjectGraph() {
Timber.d("Initializing application object graph!");
og = ObjectGraph.create(Modules.list(this));
og.inject(this);
}
在OverviewFragment,我檢索此創建對象圖(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);
}
}
爲什麼我的應用程序說我的天堂即使我明確聲明我正在向OverviewModule中的OverviewFragment注入內容,也沒有註冊?
嗨,只是對刪除線中的文字感興趣。這是爲什麼? – lemuel 2015-03-10 06:00:42