본문으로 바로가기

https://stackoverflow.com/questions/48872985/dagger2-no-injector-factory-bound-for-fragment 참고

 

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.myapp/com.myapp.main.MainActivity}: java.lang.IllegalArgumentException: No injector factory bound for Class<com.myapp.ui.main.Home.HomeFragment>
      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2665)
      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
      at android.app.ActivityThread.-wrap12(ActivityThread.java)
      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
      at android.os.Handler.dispatchMessage(Handler.java:102)
      at android.os.Looper.loop(Looper.java:154)
      at android.app.ActivityThread.main(ActivityThread.java:6119)
      at java.lang.reflect.Method.invoke(Native Method)
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
Caused by: java.lang.IllegalArgumentException: No injector factory bound for Class<com.myapp.ui.main.Home.HomeFragment>
      at dagger.android.DispatchingAndroidInjector.inject(DispatchingAndroidInjector.java:104)
      at dagger.android.support.AndroidSupportInjection.inject(AndroidSupportInjection.java:74)
      at com.myapp.ui.main.Home.HomeFragment.onAttach(HomeFragment.java:65)
      at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1363)
      at android.support.v4.app.FragmentTransition.addToFirstInLastOut(FragmentTransition.java:1109)
      at android.support.v4.app.FragmentTransition.calculateFragments(FragmentTransition.java:996)
      at android.support.v4.app.FragmentTransition.startTransitions(FragmentTransition.java:99)
      at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2364)
      at android.support.v4.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2322)
      at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2229)
      at android.support.v4.app.FragmentManagerImpl.dispatchStateChange(FragmentManager.java:3221)
      at android.support.v4.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManager.java:3171)
      at android.support.v4.app.FragmentController.dispatchActivityCreated(FragmentController.java:192)
      at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:560)
      at android.support.v7.app.AppCompatActivity.onStart(AppCompatActivity.java:177)
      at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1248)
      at android.app.Activity.performStart(Activity.java:6696) ...

 

Dagger2를 사용하면 No injector factory bound for Class 에러가 발생하는 경우

 

inject를 사용하고자 하는 Fragment에 HasSupportFragmentInjector를 구현하고 onAttach에서 AndroidSupportInjection.inject(this)를 실행 시켜준다.

public class HomeFragment extends Fragment implements HasSupportFragmentInjector {
    ....
    @Override
    public void onAttach(Context context) {
        AndroidSupportInjection.inject(this);
        super.onAttach(context);
    }
    ....
}

Activity에서도 DaggerAppCompatActivity를 상속하거나 HasSupportFragmentInjector를 구현하고 onCreate에서 AndroidSupportInjection.inject(this)를 실행

public class MainActivity extends AppCompatActivity implements HasSupportFragmentInjector {

  @Inject MainActivityContract.Presenter mPresenter;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
      AndroidInjection.inject(this);
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
  }
  
  ....
  
}

아래와 같은 코드로 해당 Activity와 Fragment에 inject을 해야한다.

@Module
public abstract class ActivityBindingModule {

    @ActivityScope
    @ContributesAndroidInjector(modules = MainActivityModule.class)
    abstract MainActivity bindMainActivity();
    
    ....
}
@Module
public abstract class MainActivityModule {
    ....

    @FragmentScope
    @ContributesAndroidInjector
    abstract HomeFragment provideHomeFragment();
    
    ....
    
}