无法使用匕首2注入碎片

时间:2018-10-09 19:09:11

标签: android android-fragments dagger-2

我能够注入Activity,BroadcastReceviers等,但是当我尝试以相同方式注入Fragment时,出现以下异常:

.../AppComponent.java:8: error: [Dagger/MissingBinding] [dagger.android.AndroidInjector.inject(T)] java.util.Map<java.lang.String,javax.inject.Provider<dagger.android.AndroidInjector.Factory<? extends android.support.v4.app.Fragment>>> cannot be provided without an @Provides-annotated method.
    public abstract interface AppComponent extends dagger.android.AndroidInjector<com.inepex.simpleclient.App> {
                    ^
          java.util.Map<java.lang.String,javax.inject.Provider<dagger.android.AndroidInjector.Factory<? extends android.support.v4.app.Fragment>>> is injected at
              dagger.android.DispatchingAndroidInjector.<init>(…, injectorFactoriesWithStringKeys)
          dagger.android.DispatchingAndroidInjector<android.support.v4.app.Fragment> is injected at
              com.inepex.tracker.SimpleTrackerApp.fragmentInjector
          com.inepex.simpleclient.App is injected at
              dagger.android.AndroidInjector.inject(T)

我遵循以下描述: https://code.luasoftware.com/tutorials/android/dagger2-injection-for-fragment/

更新

我想要一个Fragment实例,因为我想像这样切换片段:

Fragment fragment = new WelcomeFragment();

FragmentManager fm = getSupportFragmentManager();
FragmentTransaction transaction = fm.beginTransaction();
transaction.replace(R.id.contentFragment, fragment);
transaction.commit();

注入片段:

class MainActivity : BaseActivity() {

    @Inject
    lateinit var welcomeFragment: WelcomeFragment

    override fun onCreate(savedInstanceState: Bundle?) {
        AndroidInjection.inject(this)
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        ButterKnife.bind(this)
    }
}

组件:

@Component(modules = arrayOf(AndroidInjectionModule::class, AppModule::class, AbstractAppModule::class, AbstractLibModule::class, LibModule::class))
@Singleton
interface AppComponent : AndroidInjector<App> {

    @Component.Builder
    interface Builder {
        @BindsInstance
        fun application(application: App): Builder

        fun build(): AppComponent
    }
}

模块:

@Module
abstract class AbstractAppModule {

    @ContributesAndroidInjector
    internal abstract fun welcomeFragment(): WelcomeFragment

    @ContributesAndroidInjector
    internal abstract fun mainActivity(): MainActivity

    @ContributesAndroidInjector
    internal abstract fun onboardingActivity(): OnboardingActivity
}

应用程序:

open class SimpleTrackerApp : MultiDexApplication(),
        HasActivityInjector,
        HasServiceInjector,
        HasBroadcastReceiverInjector,
        HasSupportFragmentInjector {


    @Inject
    lateinit var dispatchingAndroidInjector: DispatchingAndroidInjector<Activity>

    @Inject
    lateinit var dispatchingServiceInjector: DispatchingAndroidInjector<Service>

    @Inject
    lateinit var dispatchingBroadcastReceiverInjector: DispatchingAndroidInjector<BroadcastReceiver>

    @Inject
    lateinit var fragmentInjector: DispatchingAndroidInjector<Fragment>

    override fun activityInjector(): AndroidInjector<Activity> = dispatchingAndroidInjector

    override fun serviceInjector(): AndroidInjector<Service> = dispatchingServiceInjector

    override fun broadcastReceiverInjector(): AndroidInjector<BroadcastReceiver> =
            dispatchingBroadcastReceiverInjector

    override fun supportFragmentInjector(): DispatchingAndroidInjector<Fragment> = fragmentInjector

2 个答案:

答案 0 :(得分:0)

已更新:

我注意到您使用的是android.support.v4.app.Fragment,但是您使用的是来自应用程序的片段。您需要添加

implementation 'com.google.dagger:dagger-android-support:2.x' // if you use the support libraries

此外,活动需要实现HasFragmentInjector接口

public class YourActivity extends Activity
    implements HasFragmentInjector {
  @Inject DispatchingAndroidInjector<Fragment> fragmentInjector;

  @Override
  public void onCreate(Bundle savedInstanceState) {
    AndroidInjection.inject(this);
    super.onCreate(savedInstanceState);
    // ...
  }

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

已更新:

首先,您需要在创建之前注入片段

  

最后,在您的Activity.onCreate()方法中,调用   调用super.onCreate();之前的AndroidInjection.inject(this):

public class YourActivity extends Activity {
  public void onCreate(Bundle savedInstanceState) {
    AndroidInjection.inject(this);
    super.onCreate(savedInstanceState);
  }
}

Docs

您添加了此模块吗?

@Module abstract class FragmentModule {
    @ContributesAndroidInjector
    internal abstract fun contributePhotoPinListFragment(): PhotoPinListFragment
}

至AppComponent:

@Singleton
@Component(modules = [..., (FragmentModule::class)])
interface AppComponent {

答案 1 :(得分:0)

好像您没有在模块中为Fragment添加提供程序。 Dagger不知道如何找到您要注入的Fragment的对象。因此,在模块类内部,您必须创建一个如下的provide(仅作为示例)

@Provides
@Singleton
fun provideFragment(): Fragment = Fragment()