loe*_*chg 1 java mvp android dependency-injection dagger
我是Dagger的新手,并试图找出为什么没有注入特定的依赖.尝试在片段中注入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)
Run Code Online (Sandbox Code Playgroud)
...
我有一个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;
}
}
Run Code Online (Sandbox Code Playgroud)
我有一个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);
}
}
Run Code Online (Sandbox Code Playgroud)
对象图是在BaseApplication对象的onCreate(带有Review模块)中创建并注入的(Modules.list()返回模块列表,当前是空白的TestReviewsModule和上面看到的ReviewsModule ...).
private void initializeObjectGraph() {
Timber.d("Initializing application object graph!");
og = ObjectGraph.create(Modules.list(this));
og.inject(this);
}
Run Code Online (Sandbox Code Playgroud)
在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);
}
}
Run Code Online (Sandbox Code Playgroud)
为什么我的应用程序说我没有注册,即使我明确声明我在OverviewModule中注入了OverviewFragment?
Dagger/DI故障排除有点困难,所以我会尽可能彻底......也许这会帮助其他人.
我在阅读了其他一些SO问题(例如Dagger在模块上找不到可注射成员)和一些文档之后发现了这个问题.
我的基本问题是缺乏对.plus()方法的理解.我没有意识到它返回一个新的ObjectGraph而不是修改它被调用的图形.通过做一个.plus()然后注入我调用on的同一个ObjectGraph .plus(),我基本上试图从我的ReviewsModule注入...它不知道OverviewFragment,因此错误.
在OverviewFragment中,我需要进行以下更改:
// old way
void initializePresenter() {
og.plus(new OverviewModule(this)); // Note this line
og.inject(this);
mPresenter.initialize(mVendorId);
}
Run Code Online (Sandbox Code Playgroud)
至
// new way
void initializePresenter() {
og = og.plus(new OverviewModule(this)); // Note this line
og.inject(this);
mPresenter.initialize(mVendorId);
}
Run Code Online (Sandbox Code Playgroud)
这将返回包含OverviewModule和ReviewsModule的新图形.
我也不需要添加.plus()注释属性.plus().根据我的理解,.plus()是这个的运行时版本(参见前面的链接问题).
| 归档时间: |
|
| 查看次数: |
2916 次 |
| 最近记录: |