0

如何注入演示使用Dagger2如何使用Dagger2

片段我已经写了下面的代码

@Module 
public abstract class ActivityBuilder { 

    @ContributesAndroidInjector(modules = { DetailCastActivityModule.class, FragmentDependencyProvider.class }) 
    abstract DetailCastActivity bindDetailCastActivity(); 
} 

@Module 
public abstract class FragmentDependencyProvider { 

    @ContributesAndroidInjector(modules = CastInfoFragmentModule.class) 
    abstract CastInfoFragment provideCastInfoFragmentFactory(); 
} 

@Module 
public class CastInfoFragmentModule { 

    @Provides 
    CastInfoMvpPresenter<CastInfoMvpView> provideCastInfoMvpPresenter(CastInfoPresenter<CastInfoMvpView> presenter) { 
     return presenter; 
    } 

} 

,但我仍然得到以下错误,即使我已经写了提供注入演示的片段方法

Error:(24, :sunglasses: error: [dagger.android.AndroidInjector.inject(T)] com.app.nmot.ui.castdetail.info.CastInfoPresenter cannot be provided without an @Provides- or @Produces-annotated method. 
com.app.nmot.ui.castdetail.info.CastInfoPresenter is injected at 
com.app.nmot.ui.castdetail.info.CastInfoFragment.presenter 
com.app.nmot.ui.castdetail.info.CastInfoFragment is injected at 
dagger.android.AndroidInjector.inject(arg0) 

评论中提到的答案没有解决我的问题。 我已经检查了所有的注释和我使用的Dagger2

+0

的可能重复的构造函数添加@Inject [我如何解决Dagger 2错误'...不能提供\ [... \]'?](https://stackoverflow.com/questions/44912080/how-doi-i-fix-dagger-2-错误 - 不能提供) –

+0

这个解决方案不适合你吗? https://stackoverflow.com/questions/36838898/presenter-injection-with-dagger-2?answertab=votes#tab-top –

+0

@MaximeJallu - 不,我已检查所有注释,我使用新的android注射器提供Dagger2 – Passiondroid

回答

1

1提供新的Android喷油器创建范围

import java.lang.annotation.Retention; 
import java.lang.annotation.RetentionPolicy; 

import javax.inject.Scope; 

@Scope 
@Retention(RetentionPolicy.RUNTIME) 
public @interface BaseScope { 
} 

2 - 创建您的合同

public interface FeatureContract { 
    interface View { 
     void onReceiveError(Throwable throwable); 
     void onReceiveItems(List<Object> items); 
     void showAlertDialog(); 
     ... //others methods 
    } 

    interface Presenter { 
     void onInitView(Object item); 
    } 
} 

3-创建模块(dagger2)

import dagger.Module; 
import dagger.Provides; 

@Module 
public class FeatureContractModule { 

    private final FeatureContract.View mView; 

    public FeatureContractModule(FeatureContract.View view) { 
     mView = view; 
    } 

    @Provides @BaseScope 
    FeatureContract.Presenter providesFeaturePresenter(FeatureContract.View view) { 
     return new FeaturePresenter(view); 
    } 

    @Provides @BaseScope 
    FeatureContract.View providesFeatureView() { 
     return mView; 
    } 
} 

4-创建您的演示者

public class FeaturePresenter implements FeatureContract.Presenter{ 

    @NonNull 
    private final FeatureContract.View mView; 

    public FeaturePresenter(@NonNull FeatureContract.View view){ 
     mView = view; 
    } 

    @Override 
    public void onInitView(Object item){ 
     mView.showAlertDialog(); //<--for sample 
    } 
} 

5在你的片段

import javax.inject.Inject; 

public class FeatureFragment extends Fragment implements FeatureContract.View{ 
@Inject FeatureContract.Presenter mPresenter; 

@Override public void onActivityCreated(@Nullable Bundle savedInstanceState) { 
     super.onActivityCreated(savedInstanceState); 
     ((MyApplication) getActivity().getApplication()).getDataComponent() 
                   .plus(new FeatureContractModule(this /*view*/)) 
                   .inject(this /*fragment*/); 

     mPresenter. onInitView(null); 
    } 
} 

5-你的应用

public class MyApplication extends Application { 
    //Dagger object 
    private DataComponent mDataComponent; 

    /** 
    * Dagger Injector 
    */ 
    public static DataComponent get(Context context) { 
     MyApplication application = (MyApplication) context.getApplicationContext(); 
     return application.mDataComponent; 
    } 

    @Override 
    public void onCreate() { 
    super.onCreate(); 
    mDataComponent = DaggerDataComponent.builder() 
             .dataModule(new DataModule(this, Locale.getDefault())) 
             .build(); 
    } 
    public DataComponent getDataComponent() { 
     return mDataComponent; 
    } 
} 

6 - 创建DataComponent

import javax.inject.Singleton; 

import dagger.Component; 

@Singleton 
@Component(modules = {DataModule.class}) 
public interface DataComponent { 
Application application(); 
FeatureComponent plus(FeatureContractModule module); 
... 
} 

7 - 最后的ComponentModule

import dagger.Module; 
import dagger.Provides; 

@BaseScope 
@Subcomponent(modules = FeatureContractModule.class) 
public interface FeatureComponent { 
    void inject(FeatureFragment fragment); 
} 

我相信我还没有忘记任何东西

+0

请参阅博客:[https://android.jlelse.eu/android-mvp-architecture-with-dependency-injection-dee43fe47af0](MVP_Dagger2 Article1) 或者:[https://www.raywenderlich.com/146804/依赖注入匕首-2](MVP_Dagger2 Article2) –

0

让您CastInfoFragmentModule摘要和补充一点:

@YourScope 
@Binds 
abstract CastInfoMvpPresenter<CastInfoMvpView> bindPresenter(CastInfoPresenter<CastInfoMvpView> presenter); 

而且,你必须在CastInfoPresenter

+0

我已经有@Inject CastInfoPresenter的构造函数。你能告诉我什么是自定义范围的需要吗? – Passiondroid

+0

因此,您总是在组件的生命周期中获得相同的对象 – Benjamin