错误:[Dagger / MissingBinding] [dagger.android.AndroidInjector.inject(T)] Dagger

时间:2018-08-22 10:47:03

标签: java android dagger-2 dagger android-mvp

我想制作一个包含一个主要活动和多个片段的简单项目。 在这里,我在一次活动中有两个片段,我想注入演示者来登录片段,但是它不起作用。我的错误在哪里?

MainApplication.java

public class MainApplication extends DaggerApplication{


private static ApplicationComponent component;
@Override
public void onCreate() {
    super.onCreate();
    Utils.init(this);
}
public static ApplicationComponent getComponent() {
    return component;
}


protected AndroidInjector<? extends DaggerApplication> applicationInjector() 
{
    component = 
    DaggerApplicationComponent.builder().application(this).build();
    component.inject(this);
    return component;
  }


}

MainActivity.java

public class MainActivity extends DaggerAppCompatActivity  {



private Fragment[] mFragments = new Fragment[2];
private int curIndex;

@Inject
HomeFragment homeFragment;

@Inject
LoginFragment loginFragment;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    if (savedInstanceState != null) {
        curIndex = savedInstanceState.getInt("curIndex");
    }
    mFragments[AppConstant.HOME_FRAGMENT] = homeFragment;
    mFragments[AppConstant.LOGIN_FRAGMENT] = loginFragment;

    FragmentUtils.add(getSupportFragmentManager(), mFragments, 
  R.id.container, curIndex);
    showCurrentFragment(AppConstant.LOGIN_FRAGMENT);
}
private void showCurrentFragment(int index) {
    FragmentUtils.showHide(curIndex = index, mFragments);
}

@Override
public void onSaveInstanceState(Bundle outState, PersistableBundle 
  outPersistentState) {
    super.onSaveInstanceState(outState, outPersistentState);
    outState.putInt("curIndex", curIndex);
  }


 }

ApplicationComponent.java

@Singleton
@Component(modules = {
    ContextModule.class,
    ApiServiceModule.class,
    AndroidSupportInjectionModule.class,
    ActivityBuilder.class
 })
  public interface ApplicationComponent extends 
         AndroidInjector<DaggerApplication> {

void inject(MainApplication component);

@Component.Builder
interface Builder {

    ApplicationComponent build();

    @BindsInstance
    Builder application(MainApplication application);
}
}

LoginFragment.java

public class LoginFragment extends DaggerFragment{

View mRootView;


@Inject
HomeFragment homeFragment;


@Inject
LoginContract.Presenter mPresenter;

@Inject
public LoginFragment() {
    // Required empty public constructor
}


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    mRootView =inflater.inflate(R.layout.fragment_login, container, false);
    ButterKnife.bind(this,mRootView);
    // Inflate the layout for this fragment
    return mRootView;
}

@OnClick(R.id.button)
public void goToHome(){
    FragmentUtils.replace(this,homeFragment,false);

}

@Override
public void onAttach(Context context) {
    super.onAttach(context);

}

@Override
public void onDetach() {
    super.onDetach();

     }


  }

ActivityBuilder.java

@Module
public abstract class ActivityBuilder {

@ContributesAndroidInjector(modules = {MainActivityModule.class , 
FragmentContainerModule.class, AndroidSupportInjectionModule.class} )
abstract MainActivity bindMainActivity();


 }

FragmentContainerModule.java

@Module
public abstract class FragmentContainerModule {


 @ContributesAndroidInjector(modules = 
             {HomeFragmentModule.class,
              AndroidSupportInjectionModule.class})
abstract HomeFragment contributeHomeFragmentInjector();


@FragmentScope
@ContributesAndroidInjector(modules = 
           {LoginFragmentModule.class,
             AndroidSupportInjectionModule.class})
abstract LoginFragment contributeLoginFragmentInjector();




}

** LoginFragmentModule.java

@Module
public abstract class LoginFragmentModule {

@FragmentScope
@Binds
public abstract LoginContract.Presenter presenter(LoginPresenter 
      loginPresenter);
}

这是错误:

error: [Dagger/MissingBinding] [dagger.android.AndroidInjector.inject(T)] 
ir.heart.heartproject.utils.MvpUtils.LoginContract.Presenter cannot be 
provided without an @Provides-annotated method.
A binding with matching key exists in component: 
ir.heart.heartproject.utils.di.
modules.FragmentContainerModule_ContributeLoginFr 
agmentInjector.LoginFragmentSubcomponent
ir.heart.heartproject.utils.MvpUtils.
LoginContract.Presenter is injected at
ir.heart.heartproject.views.fragments.LoginFragment.mPresenter
ir.heart.heartproject.views.fragments.LoginFragment is injected at
ir.heart.heartproject.views.activities.MainActivity.loginFragment
ir.heart.heartproject.views.activities.MainActivity is injected at
dagger.android.AndroidInjector.inject(T)
component path: 
ir.heart.heartproject.utils.di.component.ApplicationComponent ? 
ir.heart.heartproject.utils.di.modules.
ActivityBuilder_BindMainActivity.MainActivitySubcomponent

1 个答案:

答案 0 :(得分:4)

您可以使用@inject注释LoginFragment的构造函数,当请求一个新实例时,Dagger会构造LoginFragment类的实例并满足其依赖性(例如mPresenter字段)。由于MainActivitySubcomponent无法访问LoginFragmentModule,因此Dagger会为您提供一个无法提供LoginContract.Presenter的错误。如果您使用其他方法为MainActivity提供LoginFragment,而不是使用构造函数注入,则将解决此问题。您应该从LoginFragment构造函数中删除@inject并创建一个模块来提供它,如以下示例所示:

@Module
public class MainModule {

    @Provides
    public static LoginFragment provideLoginFragment() {
        return LoginFragment.newInstance("param1", "param2");
    }

}

Dagger技巧:不要对系统实例化它的对象使用构造方法注入。在android中,要在活动和片段中注入依赖关系,您应该使用字段注入方法。