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

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

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

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

您使用@inject 来注解LoginFragment 的构造函数,当请求一个新的实例时,Dagger 会构造您的LoginFragment 的实例class 并满足它们的依赖关系(如mPresenter 字段)。因为MainActivitySubcomponent没有访问LoginFragmentModule,所以Dagger给你一个无法提供的错误LoginContract.Presenter。如果您使用另一种方法为 MainActivity 提供 LoginFragment,而不是使用构造函数注入,问题将得到解决。您应该从 LoginFragment 构造函数中删除 @inject 并创建一个模块来提供它,如下例所示:

@Module
public class MainModule {

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

}

匕首提示:不要对系统实例化的对象使用构造函数注入。在 android 中,要在 activity 和片段中注入依赖项,您应该使用字段注入方法。