我有以下形式的活动:
public class MyActivity<T extends AbstractDescriptionItem> extends BaseActivity[...]
当我定义:
@ContributesAndroidInjector
abstract MyActivity contributeMyActivity();
我收到此错误:
error: [Dagger/MembersInjection] [dagger.android.AndroidInjector.inject(T)] Cannot inject members into raw type com.test MyActivity
如果我尝试:
@ContributesAndroidInjector
abstract MyActivity<AbstractDescriptionItem> contributeMyActivity();
我得到了
error: @ContributesAndroidInjector methods cannot return parameterized types
有没有办法在不更改架构的情况下在MyActivity中注入依赖项?
答案 0 :(得分:0)
我想出的解决方案是使用类型参数实现Activity / Fragment的不同情况并贡献实现。对于您的情况,将是这样的:
public abstract class MyActivity<T extends AbstractDescriptionItem> extends BaseActivity[...]
假设您有一个BookDescriptionItem
类扩展了AbstractDescriptionItem
:
public class BookDescriptionActivity extends MyActivity<BookDescriptionItem>
最后,贡献实现:
@ContributesAndroidInjector
abstract BookDescriptionActivity contributeBookDescriptionActivity();