gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:55:45 31 4
gpt4 key购买 nike

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

主应用程序.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;
}


}

主 Activity .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);
}


}

应用组件.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);
}
}

登录 fragment .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();


}

fragment 容器模块.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 类的实例并满足它们的依赖关系(如mPresenter 字段)。因为 MainActivitySubcomponent 没有访问 LoginFragmentModule,Dagger 报错不能提供 LoginContract.Presenter。如果您使用另一种方法为 MainActivity 提供 LoginFragment,而不是使用构造函数注入(inject),问题将得到解决。您应该从 LoginFragment 构造函数中删除 @inject 并创建一个模块来提供它,如下例所示:

@Module
public class MainModule {

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

}

Dagger 提示:不要对系统实例化它的对象使用构造函数注入(inject)。在android中,为了在activity和fragment中注入(inject)依赖,你应该使用字段注入(inject)方法。

关于java - 错误 : [Dagger/MissingBinding] [dagger. android.AndroidInjector.inject(T)] Dagger,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51965146/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com