我對Dagger 2相當陌生,而且我有以下類。沒有@Inject構造函數就不能提供Dagger 2對象
我有2個模塊:
DaoSessionModule
@Module
public class DaoSessionModule {
private DaoSession daoSession;
private Context context;
public DaoSessionModule(Context context) {
this.context = context;
if(daoSession == null) {
DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(this.context, "my_pocket");
Database db = helper.getWritableDb();
daoSession = new DaoMaster(db).newSession();
}
}
@Provides
LanguageDao providesLanguageDao() {
return daoSession.getLanguageDao();
}
@Provides
CategoryDao providesCategoryDao() {
return daoSession.getCategoryDao();
}
}
和GlobalPrefModule
@Module
public class GlobalPrefModule {
private GlobalPref globalPerf;
public GlobalPrefModule(GlobalPref globalPerf) {
this.globalPerf = globalPerf;
}
@Provides
public GlobalPref providesGlobalPref() {
return this.globalPerf;
}
}
及其組件去,因爲:
@Singleton
@Component(modules = {DaoSessionModule.class})
public interface DaoSessionComponent {
void inject(SplashActivity activity);
}
和
@Singleton
@Component(modules = {GlobalPrefModule.class })
public interface GlobalPrefComponent {
void inject(SplashActivity activity);
}
和我在應用程序類建立兩個:
daoSessionComponent = DaggerDaoSessionComponent.builder()
.daoSessionModule(new DaoSessionModule(this))
.build();
globalPrefComponent = DaggerGlobalPrefComponent.builder()
.globalPrefModule(new GlobalPrefModule(new GlobalPref()))
.build();
,並在我的飛濺活動注入其中:
public class SplashActivity extends BaseActivity {
@Inject
LanguageDao languageDao;
@Inject
GlobalPref globalPerf;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
initInjections();
}
private void initInjections() {
ZoopiApplication.app().getDaoSessionComponent().injectDao(this);
ZoopiApplication.app().getGlobalPrefComponent().injectGlobalPref(this);
}
}
我現在面臨的問題是如果我只是在我的注入中注入DaoSession並註釋掉GlobalPref impl,它會簡單地工作,但是當我將GlobalPref與Daos一起添加時分裂國家它不能建立並給了我以下錯誤消息:
Error:(8, 52) error: cannot find symbol class DaggerDaoSessionComponent
Error:(9, 52) error: cannot find symbol class DaggerGlobalPrefComponent
Error:(16, 10) error: mypocket.com.zoopi.GlobalPref cannot be provided without an @Inject constructor or from an @Provides- or @Produces-annotated method.
mypocket.com.zoopi.GlobalPref is injected at
mypocket.com.zoopi.activities.SplashActivity.globalPerf
mypocket.com.zoopi.activities.SplashActivity is injected at
mypocket.com.zoopi.dagger.dagger2.components.DaoSessionComponent.injectDao(activity)
Error:(16, 10) error: mypocket.com.zoopi.models.LanguageDao cannot be provided without an @Inject constructor or from an @Provides- or @Produces-annotated method.
mypocket.com.zoopi.models.LanguageDao is injected at
mypocket.com.zoopi.activities.SplashActivity.languageDao
mypocket.com.zoopi.activities.SplashActivity is injected at
mypocket.com.zoopi.dagger.dagger2.components.GlobalPrefComponent.injectGlobalPref(activity)
兩者生成的類DaggerDaoSessionComponent和DaggerGlobalPrefComponent在構建foloder產生。
什麼可能是我不能將兩個對象注入同一活動的原因?
您是否嘗試過使用兩個模塊的單個組件? –
@RuwankaMadhushan我想到了,但是我很快就會有更多的模塊,我不知道是否有一個組件處理所有模塊是一個好主意......我不明白爲什麼這不會工作 –