2 enjeksiyonlu bir Etkinliğim var. Her enjekte bileşen tek başına çalışır, ancak enjekte hem aşağıdaki hatalara yol açmaktadır:Hançer 2 Android'de çoklu Enjeksiyon
Error:(12, 10) error: android.app.Fragment cannot be provided without an @Inject constructor or from an @Provides- or @Produces-annotated method.
fr.daggertest.MainActivity.fragmentB
[injected field of type: android.app.Fragment fragmentB]
Error:(12, 10) error: android.app.Application cannot be provided without an @Inject constructor or from an @Provides- or @Produces-annotated method.
fr.daggertest.MainActivity.applicationA
[injected field of type: android.app.Application applicationA]
Ama ikisi de zaten açıklamalı @Provides, bu yüzden neyin yanlış olduğunu görmüyorum?
MainActivity.java
public class MainActivity extends AppCompatActivity {
@Inject
Fragment fragmentB;
@Inject
Application applicationA;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// create component & inject...
}
Modüller & bileşenleri:
@Component(modules={ModuleA.class})
@Singleton
public interface ComponentA {
void inject(MainActivity activity);
}
@Component(modules={ModuleB.class})
@Singleton
public interface ComponentB {
void inject(MainActivity activity);
}
@Module
public class ModuleA {
Application mApplication;
public ModuleA(Application application) {
mApplication = application;
}
@Provides
public Application provideApplication() {
return mApplication;
}
}
@Module
public class ModuleB {
Fragment mFragment;
public ModuleB(Fragment fragment) {
mFragment = fragment;
}
@Provides
public Fragment provideFragment() {
return mFragment;
}
}
"Activity", sadece "Fragment" ve "Application" sağlayan bir yöntem sağlamadınız. Ayrıca "create component & injject" kodunuzu da gönderebilir misiniz? – Pelocho
Üzgünüm, bir yazım hatasıydı, kodu düzenledim. "Oluşturma bileşeni ve enjekte etme" kodu yorumlanır, derleme hatasının nedeni bu değil. –
Aslında, olabilir. Cevabımı kontrol edin – Pelocho