2
我正在編寫一個應用程序,我正在使用Dagger2進行依賴注入。我是匕首的新手。我對它的工作原理感到困惑,我無法弄清楚如何使用這個庫。我已經嘗試在博客文章的幫助下寫下模塊和組件。我已經提到了該模塊的用途。如果我錯了,請糾正我。無法訪問Nullable Dagger2 Android
APP模塊和組件:應該產生在整個應用單一實例,我想用這個例子來訪問某些Android資源AppComponent
的@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(); }
實例化
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); }
我收到以下錯誤
error: cannot access Nullable class file for javax.annotation.Nullable not found
在下面類使用@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);
}
爲什麼我得到那個錯誤?任何幫助,將不勝感激。提前致謝。
有一些缺失代碼片段。編譯時或執行時是否出錯? – Jileshl
我正在編譯期間收到它,即當我在Android Studio中單擊運行應用程序時 – nithesh1990