android - 没有找到 fragment dagger 2.11 的注入(inject)器

标签 android

我有一个带有一个 fragment 的 Activity 。我正在尝试注入(inject) fragment ,但我得到“没有为 com.tsiro.dogvip.login.signin.SignInFrgmt 找到注入(inject)器”异常。

Activity 模块:

@Module(includes = BaseActivityModule.class)
public abstract class LoginActivityModule {

   @PerFragment
   @ContributesAndroidInjector(modules = SignInFragmentModule.class)
   abstract SignInFrgmt signInFrgmtInjector();

   @Binds
   @PerActivity
   abstract Activity activity(LoginActivity loginActivity);
}

fragment 模块:

@Module(includes = BaseFragmentModule.class)
public abstract class SignInFragmentModule {

@Binds
@Named(BaseFragmentModule.FRAGMENT)
@PerFragment
abstract Fragment fragment(SignInFrgmt signInFrgmt);

}

Fragment 类扩展了实现 HasSupportFragmentInjector 的 BaseFragment。

基本 fragment :

public abstract class BaseFragment extends Fragment implements HasSupportFragmentInjector, Lifecycle.View {

@Inject
DispatchingAndroidInjector<Fragment> fragmentInjector;
public abstract Lifecycle.ViewModel getViewModel();

@SuppressWarnings("deprecation")
@Override
public void onAttach(Activity activity) {
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
        // Perform injection here before M, L (API 22) and below because onAttach(Context)
        // is not yet available at L.
        AndroidSupportInjection.inject(this);
    }
    super.onAttach(activity);
}

@Override
public void onAttach(Context context) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        // Perform injection here for M (API 23) due to deprecation of onAttach(Activity).
        AndroidSupportInjection.inject(this);
    }
    super.onAttach(context);
}

@Override
public void onStart() {
    super.onStart();
    getViewModel().onViewAttached(this);
}

@Override
public void onResume() {
    super.onResume();
    getViewModel().onViewResumed();
}

@Override
public void onPause() {
    super.onPause();
}

@Override
public void onStop() {
    super.onStop();
    getViewModel().onViewDetached();
}

@Override
public AndroidInjector<Fragment> supportFragmentInjector() {
    return fragmentInjector;
}

 }

谁能告诉我我错过了什么?

最佳答案

您的代码的问题是接口(interface) HasSupportFragmentInjectorHasFragmentInjector 的错误实现(这取决于您是否使用 fragment 的支持库)。

您应该在托管 fragment 的 Activity 或 Application 类中实现此接口(interface)。就个人而言,我会推荐以下 Application 类,这样您就不必费心在每个托管 Fragment 的 Activity 上实现它:

public class MyApplication extends Application implements HasActivityInjector, HasSupportFragmentInjector {

    @Inject
    DispatchingAndroidInjector<Activity> mActivityInjector;

    @Inject
    DispatchingAndroidInjector<Fragment> mFragmentInjector;

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

        //The way you build your top-level Application component can vary. This is just an example
        DaggerApplicationComponent.builder()
            .application(this)
            .build()
            .inject(this);

    }

    @Override
    public AndroidInjector<Activity> activityInjector() {
        return mActivityInjector;
    }

    @Override
    public AndroidInjector<Fragment> supportFragmentInjector() {
        return mFragmentInjector;
    }
}

然后你注入(inject)你的 fragment (就像你已经在你的代码中所做的那样):

@Override
public void onAttach(Context context) {
    AndroidSupportInjection.inject(this);
    super.onAttach(context);
}

如果上述方法不起作用,请确保您已为您尝试注入(inject)的屏幕创建了适当的组件/子组件。如果您使用 @ContributesAndroidInjector 而不是手动定义组件,请确保您的绑定(bind)模块中有一个屏幕条目:

@ContributesAndroidInjector(modules = MyScreenModule.class) abstract MyScreenActivity bindMyScreen();

如果你仍然无法让它工作。我建议阅读实际的 Dagger documentation :

希望对你有帮助。

关于android - 没有找到 fragment dagger 2.11 的注入(inject)器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46834252/

相关文章:

android - 添加最新 Room (Android Jetpack) 时 list 合并失败

java.lang.IllegalStateException : commit already called 错误

android - 如何将 OpenSSL 指向 Android 设备上的根证书?

android - 抽屉导航犹豫

Android:Http 响应 cookie 存储

java - 在类中使用 openFileOutput()。 (不是 Activity )

Android向django服务器csrf发送post请求失败

android - 如何在游标对象保持第一行并移动到获取第二行时中断游标处理?

android - 从 uri 播放音频

java - Android开发IDE的 future ,eclipse还是Android Studio