ダガー2は新しく、次のクラスがあります。ダガー2 @Injectコンストラクターなしでオブジェクトを提供することはできません
私は2つのモジュールがあります。
@Singleton
@Component(modules = {DaoSessionModule.class})
public interface DaoSessionComponent {
void inject(SplashActivity activity);
}
:
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 = {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インプラントをコメントアウトすると、それは単に機能しますが、私はGlobalPrefをDaosと一緒に追加する瞬間ですそれは、次のエラーメッセージを構築するために失敗し、私を与えession:
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で生成されます。
私は両方のオブジェクトを同じアクティビティに挿入できないのはなぜですか?
2つのモジュールで1つのコンポーネントを試しましたか? –
@RuwankaMadhushan私はそれについて考えましたが、すぐにさらに多くのモジュールを用意していきます。すべてのモジュールを処理するコンポーネントを用意することが良いのかどうかはわかりません。作業 –