gpt4 book ai didi

android - CustomScope 可能不会引用具有不同范围的绑定(bind)

转载 作者:行者123 更新时间:2023-12-03 16:48:58 25 4
gpt4 key购买 nike

我是 Dagger 新手,我已经像这样定义了我的应用程序组件

@Singleton
@Component(modules = {ApplicationModule.class})
public interface ApplicationComponent {
void inject(BaseActivity activity);
Context context();
}

这是我的应用程序模块
@Module
public class ApplicationModule {

public TipoApplication application;

public ApplicationModule(TipoApplication application) {
this.application = application;
}

@Singleton
@Provides
public Context provideContext(){return application.getApplicationContext();}

@Singleton
@Provides
public SharedPreferences provideSharedPreferences(Context context){
return PreferenceManager.getDefaultSharedPreferences(context);
}

@Singleton
@Provides
public Gson provideGson(){
return new Gson();
}

@Singleton
@Provides
public SharedPrefsManager provideSharedPrefsManager(SharedPreferences sharedPreferences, Gson gson){
return new SharedPrefsManager(sharedPreferences, gson);
}
}

我创建了一个依赖组件 LocationProviderComponent
@LocationScope
@Component(dependencies = {ApplicationComponent.class},modules = {LocationProviderModule.class})
public interface LocationProviderComponent {
void inject(LocationRepository locationRepository);
}

最后是我的 LocationProviderModule
@Module
public class LocationProviderModule {

@Singleton
@Provides
FusedLocationProviderClient provideFusedLocationProviderClient(Context context) {
return LocationServices.getFusedLocationProviderClient(context);

}

@Singleton
@Provides
LocationRequest provideLocationRequest() {
return new LocationRequest()
.setInterval(5000)
.setFastestInterval(60000)
.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
}

@Singleton
@Provides
LocationSettingsRequest provideLocationSettingRequest(LocationRequest mLocationRequest) {
return new LocationSettingsRequest.Builder().addLocationRequest(mLocationRequest).build();
}

}

我在构建时遇到 2 个错误。

第一个找不到符号类 DaggerApplicationComponent
第二
LocationProviderComponent scoped with LocationScope may not reference bindings with different scopes:
@Singleton @Provides FusedLocationProviderClient LocationProviderModule.provideFusedLocationProviderClient(android.content.Context)
@Singleton @Provides LocationRequest .module.LocationProviderModule.provideLocationRequest()

请告诉我我做错了什么。

最佳答案

任何模块的@Provides方法只能与它们所属的组件具有相同的范围。阅读更多 here .
在你的情况下LocationProviderModuleLocationProviderComponent 的一部分范围为 @LocationScope而该模块中的提供方法使用 @Singleton范围。这正是 Dagger 所提示的:

LocationProviderComponent scoped with LocationScope may not referencebindings with different scopes


它还指出了问题所在:
@Singleton @Provides FusedLocationProviderClient LocationProviderModule.provideFusedLocationProviderClient(android.content.Context)
@Singleton @Provides LocationRequest.module.LocationProviderModule.provideLocationRequest()
而不是使用 @Singleton , 你只需要使用 @LocationScopeLocationProviderModule .

关于android - CustomScope 可能不会引用具有不同范围的绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53791779/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com