有一个通用的片段
public class MyFragment<TYPE> extends Fragment {
@Inject
SomeClass class;
public MyFragment(){}
}
}我无法将此片段添加到片段绑定模块,因为Dagger抱怨它是原始类型。
如何在绑定模块中提到类型类?
我的绑定模块现在看起来是这样的:
@Module
public abstract class BindingModule {
@ContributesAndroidInjector(modules = MyFragmentModule.class)
abstract MyFragment bindMyFragment();
} 错误
error: [dagger.android.AndroidInjector.inject(T)] MyFragment has type parameters, cannot members inject the raw type.发布于 2018-08-24 21:03:21
首先我要说的是,fragment是一个特殊的类,它的实例化必须由OS来完成,通过将它添加到您的模块中,您可以绕过必须由OS控制的fragment的生命周期。
https://google.github.io/dagger/android
但是,如果您希望按模块提供它,则必须对泛型类型使用限定符。更改您的模块,如下所示:
@Module
public abstract class BindingModule {
@ContributesAndroidInjector(modules = MyFragmentModule.class)
@Name('YourTypeName')
abstract MyFragment<YourType> bindMyFragment();
} 因此,无论您在何处注入它,都必须指定类型和它的限定符:
@Inject
@Name('YourTypeName')
MyFragment<YourType> fragmentReference;https://stackoverflow.com/questions/51941690
复制相似问题