【发布时间】:2018-12-20 21:31:02
【问题描述】:
我不明白如何解决此错误。在尝试将片段添加到我的应用程序并使用 Dagger for DI 后出现此错误。这是错误堆栈:
错误:[Dagger/IncompatiblyScopedBindings] di.component.ApplicationComponent (unscoped) 可能不引用 scoped 绑定:@Provides @di.ApplicationContext @di.ApplicationScope android.content.Context di.Module.ApplicationContextModule.getApplicationContext(application.MyApplication) @Provides @di.ApplicationScope android.content.SharedPreferences di.Module.SharedPreferencesModule.getSharedPreferences(@di.ApplicationContext android.content.Context) @Provides @di.ApplicationScope service.KeyStoreServiceInterface di.Module.KeyStoreModule.getKeyStoreService(@Named("KEY_STORE_FILE") java.io.File) @Provides @di.ApplicationScope repository.SharedPreferencesHelper di.Module.SharedPreferenceHelperModule.getSharedPreferencesHelper() @Provides @di.ApplicationScope service.CoinmarketcapService di.Module.CoinmarketcapModule.getCoinmarketcapService(com.google.gson.Gson, okhttp3.OkHttpClient) @Provides @di.ApplicationScope com.google.gson.Gson di.Module.GsonModule.getGson() @Provides @di.ApplicationScope okhttp3.OkHttpClient di.Module.OkHttpModule.getOkHttpClient() @Provides @di.ApplicationScope 存储库.WalletRepositoryInterface di.Module.WalletRepositoryModule.getWalletRepository(repository.SharedPreferencesHelper, 服务.KeyStoreService)
这是我的 ApplicationComponent 类:
@Component(modules = {ApplicationContextModule.class,
SharedPreferencesModule.class,
KeyStoreModule.class,
SharedPreferenceHelperModule.class,
AndroidInjectionModule.class,
BindModule.class,
AndroidSupportInjectionModule.class,
OkHttpModule.class,
GsonModule.class,
CoinmarketcapModule.class,
WalletRepositoryModule.class})
@SuppressWarnings("unchecked")
public interface ApplicationComponent {
@Component.Builder
interface Builder {
@BindsInstance
Builder application(MyApplication myApplication);
ApplicationComponent build();
}
void inject(MyApplication myApplication);
@ApplicationContext
Context getApplicationContext();
SharedPreferences getSharedPreferences();
KeyStoreServiceInterface getKeyStoreService();
SharedPreferencesHelper getSharedPreferencesHelper();
CoinmarketcapService getCoinmarketcapService();
WalletRepositoryInterface getWalletRepository();
}
我的 android 代码中有一个 FragmentScope 和一个 ActivityScope 注释。它只是带有 Retention.RUNTIME 的 Dagger 的常规作用域。这是我的应用程序代码:
public class MyApplication extends MultiDexApplication implements HasActivityInjector, HasFragmentInjector {
private ApplicationComponent applicationComponent;
@Inject
DispatchingAndroidInjector<Activity> dispatchingActivityInjector;
@Inject
DispatchingAndroidInjector<Fragment> fragmentDispatchingAndroidInjector;
@Override
public void onCreate() {
super.onCreate();
DaggerApplicationComponent
.builder()
.application(this)
.build()
.inject(this);
Timber.plant(new Timber.DebugTree());
}
@Override
public AndroidInjector<Activity> activityInjector() {
return dispatchingActivityInjector;
}
public ApplicationComponent getApplicationComponent() {
return applicationComponent;
}
@Override
public AndroidInjector<Fragment> fragmentInjector() {
return fragmentDispatchingAndroidInjector;
}
}
任何帮助将不胜感激。
编辑:我设法通过将@ApplicationScope 添加到我的组件来解决这个问题。为什么我必须这样做?在将片段和@FragmentScope 添加到我的代码之前(在此之前我只有@activityscope 和@applicationscope)我没有这个问题,它只是在添加片段后才出现?如果有人可以帮助回答这个问题,那么仍然值得接受这个答案来帮助其他可能有这个问题并想要理解它的人。
【问题讨论】:
标签: android dependency-injection dagger-2 dagger