Tôi không thể để MyClass
ở đây được tiêm do tính chất chung của nó.Dagger 2 Lỗi loại lớp chung loại
Dagger phàn nàn với lỗi này:
Error:(187, 10) error: com.test.MyClass has type parameters, cannot members inject the raw type. via: com.test.MyComponent.inject(com.test.MyClass obj) [component injection method for type: com.test.MyClass]
Tôi googled một chút nhưng không thể tìm thấy một giải pháp cho tình huống này.
class MyClass<Type> {
@Inject
UserCredentials userCredentials;
ResultProducer<Type> mRP;
public MyClass(ResultProducer<Type> resultProd) {
mRP = resultProd;
Injector.getComponent().inject(this);
}
public Type getResult() {
if (userCredentials.isLoggedIn()) {
mRP.get();
} else {
mRP.getAnonymousCache();
}
}
}
@Component(modules = CredentialsModule.class)
interface MyComponent {
void inject(MyClass obj);
}
@Module
class CredentialsModule {
@Provides
public UserCredentials provideUserCredentials() {
return new UserCredentials();
}
}
nên xem ra các lớp trừu tượng, các giao diện và các tham số kiểu (<>)! – vanomart