我在尝试构建/重建时遇到此错误:
error: ir.atlaspio.roshdbookstore.DI.Components.ApplicationComponent scoped with @ir.atlaspio.roshdbookstore.DI.Scopes.ApplicationScope may not reference bindings with different scopes:
@Singleton class ir.atlaspio.roshdbookstore.Data.Prefs.AtlasPreferencesHelper
我正在尝试在某些项目和教程上改善代码基础,现在我正在尝试为SharedPreferences进行依赖项注入,而我遇到了以上问题。自从我与Dagger 2 And DI搞混以来已经很长时间了,所以请原谅我的初学者问题,并帮助我找出我在哪里做错了。也有一些代码将注入SharedPrefereces
,但是我想要的是使用我自己的DataManager正确执行。
因此,这就是我创建项目的方式(基于某些教程):
说明部分(对您来说这可能很明显):添加了一些将数据设置为sharedpref的方法。 AtlasPreferencesHelper:
@Singleton
public class AtlasPreferencesHelper implements PreferencesHelper {
private static final String PREF_KEY_USER_LOGGED_IN_MODE = "PREF_KEY_USER_LOGGED_IN_MODE";
private static final String PREF_KEY_FIRST_TIME_RUN_MODE = "PREF_KEY_FIRST_TIME_RUN_MODE";
private final SharedPreferences atlasPrefs;
@Inject
public AtlasPreferencesHelper(@ApplicationContext Context context,
@PreferenceInfo String prefFileName) {
atlasPrefs = context.getSharedPreferences(prefFileName, Context.MODE_PRIVATE);
}
@Override
public int getCurrentUserLoggedInMode() {
return atlasPrefs.getInt(PREF_KEY_USER_LOGGED_IN_MODE,
DataManager.LoggedInMode.LOGGED_IN_MODE_LOGGED_OUT.getType());
}
@Override
public void setCurrentUserLoggedInMode(DataManager.LoggedInMode mode) {
atlasPrefs.edit().putInt(PREF_KEY_USER_LOGGED_IN_MODE, mode.getType()).apply();
}
@Override
public int getCurrentFirstTimeStat() {
return atlasPrefs.getInt(PREF_KEY_FIRST_TIME_RUN_MODE,
DataManager.FirstTimeRun.FIRST_TIME_RUN_TRUE.getFrType());
}
@Override
public void setCurrentFirstTimeStat(DataManager.FirstTimeRun fMode) {
atlasPrefs.edit().putInt(PREF_KEY_FIRST_TIME_RUN_MODE, fMode.getFrType()).apply();
}
}
说明:要在AtlasPreferencesHelper中实现的接口。 PreferenceHelper:
public interface PreferencesHelper {
int getCurrentUserLoggedInMode();
void setCurrentUserLoggedInMode(DataManager.LoggedInMode mode);
int getCurrentFirstTimeStat();
void setCurrentFirstTimeStat(DataManager.FirstTimeRun fMode);
}
说明:做好工作并获得使用方法的活动的权限。 AppDataManager:
public class AppDataManager implements DataManager {
private static final String TAG = "AppDataManager";
private final Context mContext;
private final PreferencesHelper mPrefencesHelper;
@Inject
public AppDataManager(@ApplicationContext Context context, PreferencesHelper prefencesHelper) {
mContext = context;
mPrefencesHelper = prefencesHelper;
}
@Override
public void setUserAssLoggedOut() {
setCurrentUserLoggedInMode(LoggedInMode.LOGGED_IN_MODE_LOGGED_OUT);
}
@Override
public int getCurrentUserLoggedInMode() {
return mPrefencesHelper.getCurrentUserLoggedInMode();
}
@Override
public void setCurrentUserLoggedInMode(LoggedInMode mode) {
mPrefencesHelper.setCurrentUserLoggedInMode(mode);
}
@Override
public int getCurrentFirstTimeStat() {
return mPrefencesHelper.getCurrentFirstTimeStat();
}
@Override
public void setCurrentFirstTimeStat(FirstTimeRun fMode) {
mPrefencesHelper.setCurrentFirstTimeStat(fMode);
}
}
说明:已实现AppDataManager的DataManager接口 数据管理器:
public interface DataManager extends PreferencesHelper {
void setUserAssLoggedOut();
enum LoggedInMode {
LOGGED_IN_MODE_LOGGED_OUT(0),
LOGGED_IN_MODE_SERVER(1);
private final int mType;
LoggedInMode(int type)
{
mType = type;
}
public int getType()
{
return mType;
}
}
enum FirstTimeRun {
FIRST_TIME_RUN_FALSE(0),
FIRST_TIME_RUN_TRUE(1);
private final int frType;
FirstTimeRun(int rType){
frType = rType;
}
public int getFrType()
{
return frType;
}
}
}
说明:上下文提供上下文,改造提供改造和SharedPref的组件将提供Datamanager和SharedPref ApplicationComponent:
@ApplicationScope
@Component(modules = {ContextModule.class,
RetrofitModule.class,
SharedPrefModule.class})
public interface ApplicationComponent {
MyAtlasAPI getApiReference();
DataManager getDataManager();
@ApplicationContext
Context getContext();
void injectApplication(AtlasApplication atlasApplication);
}
说明:将在datamanager和sharedpref的组件中实现的模块 SharedPrefModule:
@Module
public class SharedPrefModule {
@Provides
@PreferenceInfo
String providePreferenceName() {
return AppConstants.PREF_NAME;
}
@Provides
@Singleton
DataManager provideDataManager(AppDataManager appDataManager)
{
return appDataManager;
}
@Provides
@Singleton
PreferencesHelper providePreferencesHelper(AtlasPreferencesHelper atlasPreferencesHelper)
{
return atlasPreferencesHelper;
}
}
答案 0 :(得分:1)
您得到的错误说明了一切。您正在尝试引用范围与组件不同的模块。
看着你ApplicationComponent
:
@ApplicationScope
@Component(modules = {ContextModule.class,
RetrofitModule.class,
SharedPrefModule.class})
public interface ApplicationComponent { }
它正在使用ApplicationScope
您的ApplicationComponent然后引用了SharedPrefModule
。
如果我们看一下该课程:
@Module
public class SharedPrefModule {
@Provides
@Singleton
PreferencesHelper providePreferencesHelper(AtlasPreferencesHelper atlasPreferencesHelper) {
return atlasPreferencesHelper;
}
}
该模块正在使用Singleton
范围。
最后,您的AtlasPreferencesHelper
的范围也为Singleton
:
@Singleton
public class AtlasPreferencesHelper {}
您想做的就是对齐这些示波器。
因此,用ApplicationComponent
注释@Singleton
或对@Application
ApplicationComponent