[英]dagger2 injection not working - SharedPreferences
我正在嘗試使用Dagger2進行Dependancy Injection。 它在構建階段給出了錯誤,並表示無法注入SharedPreference實例。
這是我的模塊和組件。
Application Module
@Module
public class ApplicationModule {
private Application app;
private String PREF_NAME = "prefs";
public ApplicationModule(Application app) {
this.app = app;
}
@Singleton
@Provides
public Picasso getPicasso() {
return new Picasso.Builder(app).build();
}
@Singleton
@Provides
public SharedPreferences getAppPreferences() {
return app.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
}
}
ApplicationComponent
@Component(modules = {ApplicationModule.class})
public interface ApplicationComponent {
void inject(App app);
}
App.java
public class App extends Application {
ApplicationComponent appComponent;
@Override
public void onCreate() {
super.onCreate();
createDaggerInjections();
}
public ApplicationComponent getAppComponent() {
return appComponent;
}
public static App getAppInstance(Context context) {
return (App) context.getApplicationContext();
}
private void createDaggerInjections() {
appComponent = DaggerApplicationComponent.builder()
.applicationModule(new ApplicationModule(this))
.build();
appComponent.inject(this);
}
}
Login Module
@Module
public class LoginModule {
LoginView view;
public LoginModule(LoginView view) {
this.view = view;
}
@Provides LoginView getLoginView()
{
return view;
}
@Provides LoginPresenter getLoginPresenter(LoginView view)
{
return new LoginPresenterImpl(view);
}
}
LoginComponent
@ActivityScope
@Component(
dependencies = ApplicationComponent.class,
modules = LoginModule.class)
public interface LoginComponent {
void inject(LoginActivity activity);
LoginPresenter getLoginPresenter();
}
LoginActivity.java
public class LoginActivity extends BaseActivity implements LoginView {
private static final String TAG = "LoginActivity";
@Inject
SharedPreferences prefs;
-----
-----
-----
@Override
public void createDaggerInjections() {
DaggerLoginComponent.builder().applicationComponent(App.getAppInstance(this).getAppComponent())
.loginModule(new LoginModule(this))
.build();
}
這行@Inject SharedPreferences prefs;
給出如下錯誤。 當我嘗試注入Picasso Instance時也會出現同樣的錯誤。
/home/blackidn/proj/styling android 3/dagger 2/DaggerEx/app/src/main/java/com/mohammad/daggerex/App/App.java:8: error: cannot find symbol
import com.mohammad.daggerex.dagger.DaggerApplicationComponent;
^
symbol: class DaggerApplicationComponent
location: package com.mohammad.daggerex.dagger
/home/blackidn/proj/styling android 3/dagger 2/DaggerEx/app/src/main/java/com/mohammad/daggerex/ui/Login/LoginComponent.java:16: error: android.content.SharedPreferences cannot be provided without an @Provides- or @Produces-annotated method.
void inject(LoginActivity activity);
^
com.mohammad.daggerex.ui.Login.LoginActivity.prefs
[injected field of type: android.content.SharedPreferences prefs]
堅持這一點,不知道如何解決這個問題並向前邁進。 我錯過了什么? 任何幫助都會很棒。
您應該將SharedPreferences公開給ApplicationComponent中的LoginComponent。 否則,你不能注入它。
@Component(modules = {ApplicationModule.class})
public interface ApplicationComponent {
void inject(App app);
SharedPreferences sharedPreferences();
}
Dagger 2更新:@Subcomponent可以在沒有任何需要的情況下使用,這里可以看到上下文:( ChatComponent是一個帶有自定義范圍的子組件)
從這篇優秀的文章: https : //proandroiddev.com/dagger-2-part-ii-custom-scopes-component-dependencies-subcomponents-697c1fa1cfc
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.