0
我使用https://guides.codepath.com/android/Dependency-Injection-with-Dagger-2的dagger2 demo。我想在DemoDaggerSubActivity.java中使用子組件進行ArrayAdapter注入。Dagger2子組件錯誤 - 其中必須註釋方法ArrayAdapter
我Application.java
@Override public void onCreate() {
super.onCreate();
mApplicationComponent = DaggerApplicationComponent.builder()
.applicationModule(new ApplicationModule(this))
.build();
}
public ApplicationComponent getApplicationComponent() {
return mApplicationComponent;
}
我MyActivityModule.java
@Module
public class MyActivityModule {
private final DemoDaggerSubActivity activity;
// must be instantiated with an activity
public MyActivityModule(DemoDaggerSubActivity activity) { this.activity = activity; }
@Provides
@MyActivityScope
@Named("my_list")
public ArrayAdapter providesMyListAdapter() {
return new ArrayAdapter<String>(activity, android.R.layout.activity_list_item);
}
}
我MyActivitySubComponent.java
@MyActivityScope
@Subcomponent(modules={ MyActivityModule.class })
public interface MyActivitySubComponent {
@Named("my_list") ArrayAdapter myListAdapter();
}
我ApplicationComponent.java
@Singleton
@Component(modules = {ApplicationModule.class})
public interface ApplicationComponent {
void inject(DemoDaggerSubActivity activity);
// factory method to instantiate the subcomponent defined here (passing in the module instance)
MyActivitySubComponent newMyActivitySubcomponent(MyActivityModule activityModule);
}
我DemoDaggerSubActivity.java
@Inject @Named("my_list")
ArrayAdapter arrayAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
((AttendanceApplication) getApplication()).getApplicationComponent().newMyActivitySubcomponent(new MyActivityModule(this)).inject(this);
}
項目後重建我有錯誤
我在哪裏標註方法ArrayAdapter?
你已經錯過了將子組件添加到圖中,從而導致錯誤 – Jileshl