You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Dagger2报错:无Fragment注入器工厂绑定,@ContributesAndroidInjector异常

Fixing "no injector factory bound for fragment" in Dagger-Android

Alright, let's tackle this frustrating error you're hitting when injecting Fragments with Dagger-Android. I've debugged this exact issue dozens of times, so let's walk through the most common fixes step by step:

Common Causes & Solutions

  • Ensure your Fragment extends DaggerFragment (AndroidX) or DaggerSupportFragment
    Dagger needs a hook into your Fragment's lifecycle to handle injection, and these base classes implement HasSupportFragmentInjector out of the box. If you're using a custom Fragment class, you'll have to manually implement that interface and delegate to the injector—but using the Dagger-provided base class is way simpler and less error-prone.

  • Verify your @ContributesAndroidInjector chain is complete
    This is the #1 mistake people make. Let's break it down:

    1. In your MainActivityModule, add a @ContributesAndroidInjector method for every Fragment tied to MainActivity:
      @Module
      public abstract class MainActivityModule {
          // Add one of these for each Fragment in MainActivity
          @ContributesAndroidInjector
          abstract HomeFragment contributeHomeFragmentInjector();
      
          // If your Fragment has its own dependencies, include its module here
          @ContributesAndroidInjector(modules = SettingsFragmentModule.class)
          abstract SettingsFragment contributeSettingsFragmentInjector();
      }
      
    2. Link this MainActivityModule to your MainActivity's injector in ActivityBindingModule:
      @Module
      public abstract class ActivityBindingModule {
          // Your working Splash/Login injectors
          @ContributesAndroidInjector
          abstract SplashActivity contributeSplashActivityInjector();
      
          @ContributesAndroidInjector
          abstract LoginActivity contributeLoginActivityInjector();
      
          // Critical: Tie MainActivity to its module that includes Fragment injectors
          @ContributesAndroidInjector(modules = MainActivityModule.class)
          abstract MainActivity contributeMainActivityInjector();
      }
      

    If you skip linking the MainActivityModule here, Dagger has no idea how to create injectors for your Fragments.

  • Double-check your ApplicationComponent includes the right modules
    For AndroidX Fragments, you must include AndroidSupportInjectionModule (not just AndroidInjectionModule) in your component's module list. Here's what it should look like:

    @Singleton
    @Component(modules = {
            AndroidInjectionModule.class,
            AndroidSupportInjectionModule.class, // Required for AndroidX Fragments
            ActivityBindingModule.class,
            // Add other global modules here
    })
    public interface ApplicationComponent extends AndroidInjector<YourApp> {
        @Component.Factory
        interface Factory extends AndroidInjector.Factory<YourApp> {}
    }
    

    Without AndroidSupportInjectionModule, Dagger can't register injectors for support/androidx Fragments.

  • Make sure MainActivity extends DaggerAppCompatActivity
    Just like the Fragment side, Dagger needs the Activity to implement HasSupportFragmentInjector to pass the injector down to child Fragments. DaggerAppCompatActivity handles this for you—don't override supportFragmentInjector() unless you know exactly what you're doing, as that can break the Dagger chain.

  • Clean and rebuild your project
    Dagger's annotation processor sometimes leaves stale generated files hanging around. Run ./gradlew clean build (or use Android Studio's Build > Clean Project → Rebuild Project) to wipe old generated code and force Dagger to regenerate injectors from scratch.

  • Avoid manual injection calls in Fragments
    If you're calling AndroidInjection.inject(this) manually in your Fragment's lifecycle, stop—DaggerFragment already does this for you in onAttach(). Duplicate injection calls can cause weird edge cases or fail if the injector isn't ready yet.

Quick Correct Configuration Recap

Let me sum up the working chain to make sure you have all pieces connected:

  1. YourApp → Uses DaggerApplication or implements HasAndroidInjector
  2. ApplicationComponent → Includes AndroidSupportInjectionModule + ActivityBindingModule
  3. ActivityBindingModule → Links MainActivity to MainActivityModule
  4. MainActivityModule → Provides @ContributesAndroidInjector for each Fragment
  5. MainActivity → Extends DaggerAppCompatActivity
  6. Your Fragments → Extend DaggerFragment

内容的提问来源于stack exchange,提问作者C. Marr

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 09:11:01