我正在编写一个使用 Dagger2 进行依赖注入的应用程序。我是匕首新手。我对它的工作原理感到困惑,我无法弄清楚如何使用这个库。我尝试借助博客中的示例编写以下模块和组件。我已经提到了该模块的目的。如果我错了,请纠正我。
应用模块和组件:它应该在整个应用程序中生成一个单例实例,我想使用这个实例来访问一些 android 资源
@Module public class AppModule { private BaseApplication baseApplication; public AppModule(BaseApplication baseApplication){ this.baseApplication = baseApplication; } @Provides @Singleton BaseApplication providesApplication(){ return baseApplication; } } @Singleton @Component(modules = {AppModule.class}) public interface AppComponent { BaseApplication getBaseApplication(); }
AppComponent 的实例化
public class BaseApplication extends Application { private AppComponent appComponent; @Override public void onCreate() { super.onCreate(); appComponent = DaggerAppComponent.builder().appModule(new AppModule(this)).build(); } public AppComponent getAppComponent(){ return appComponent; } }
上面的代码工作正常。我在下面的代码中遇到问题
位置模块和组件:如果我没有以下模块的构造函数,可以吗?
@Module public class LocationModule { @IntDef( {LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY, LocationRequest.PRIORITY_HIGH_ACCURACY, LocationRequest.PRIORITY_LOW_POWER, LocationRequest.PRIORITY_NO_POWER}) @Retention(RetentionPolicy.SOURCE) public @interface LocationPriority {} @Provides @Singleton @Named("default") LocationRequest providesLocationRequest(){ return new LocationRequest() .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY) .setInterval(Constants.LOCATION_UPDATE_INTERVAL_MS) .setFastestInterval(Constants.LOCATION_UPDATE_FASTEST_INTERVAL_MS) .setSmallestDisplacement(Constants.LOCATION_UPDATE_DISPLACEMENT_METERS); } @Provides @Singleton @Named("custom") LocationRequest providesCustomLocationRequest(int interval, int fastestInterval, @LocationPriority int priority, float updateDisplacement) { return new LocationRequest() .setPriority(priority) .setInterval(interval) .setFastestInterval(fastestInterval) .setSmallestDisplacement(updateDisplacement); } @Provides @Singleton LocationSettingsRequest providesLocationSettingsRequest(LocationRequest locationRequest){ return new LocationSettingsRequest.Builder() .addLocationRequest(locationRequest) .build(); } @Provides @Singleton FusedLocationProviderClient providesFusedLocationClient(BaseApplication baseApplication){ return LocationServices.getFusedLocationProviderClient(baseApplication); } @Provides @Singleton SettingsClient providesSettingsClient(BaseApplication baseApplication){ return LocationServices.getSettingsClient(baseApplication); } } @Singleton @Component(modules = {LocationModule.class, AppModule.class}) public interface LocationComponent { **Do we Really need a method here?** void inject(GetLocationUseCase getLocationUseCase); }
我收到以下错误
错误:无法访问 javax.annotation.Nullable 的 Nullable 类文件未找到
在下面的课程中使用@Inject 之后。如果我删除 @Inject 错误就会消失。
public final class GetLocationUseCase implements
UseCaseContract.BusinessLogic {
UseCaseContract.Mediator mediator;
@Inject
FusedLocationProviderClient fusedLocationProviderClientl;
@Override
public void onInitialize() {
BaseApplication application = mediator.requestUserContext();
DaggerLocationComponent.builder().appModule(new
AppModule(application)).locationModule(new
LocationModule()).build().inject(this);
}
为什么我会收到这个错误?任何帮助,将不胜感激。提前致谢。