I'm following the Dagger2 sample of TODO app but encounted with 2 errors.
Error1: can't find symbol DaggerNetComponent
. (Which is actually there)
Error2: Sharedpreference
can't be provided without @provider-annotated method.(Which I think results from error1)
And here's my long but simple code:
Three modules:
@Module
public class AppModule {
private final Application mApplication;
AppModule(Application application) {
mApplication = application;
}
@Provides
Application provideApplication() {
return mApplication;
}
}
@Module
public class LoadingModule {
public final LoadingContract.View mView;
public LoadingModule(LoadingContract.View mView) {
this.mView = mView;
}
@Provides
LoadingContract.View provideLoadingContractView() {
return mView;
}
}
@Module
public class NetModule {
@Provides @Singleton
SharedPreferences providesSharedPreferences(Application application) {
return PreferenceManager.getDefaultSharedPreferences(application);
}
}
And two components:
@Singleton
@Component(modules = {AppModule.class, NetModule.class})
public interface NetComponent {
}
@FragmentScoped
@Component(dependencies = NetComponent.class, modules = LoadingModule.class)
public interface LoadingComponent {
void inject(LoadingActivity loadingActivity);
}
I get NetComponent in MyApplication:
mNetComponent = DaggerNetComponent.builder()
.appModule(new AppModule(this))
.netModule(new NetModule())
.build();
and LoadingComponent in LoadingActivity:
DaggerLoadingComponent.builder()
.netComponent(((MyApplication)getApplication()).getNetComponent())
.loadingModule(new LoadingModule(loadingFragment))
.build()
.inject(this);
The only thing that I want LoadingComponent
to inject is LoadingPresenter
.
Also in LoadingActivity
:
@Inject LoadingPresenter mLoadingActivityP;
And this is how the LoadingPresenter
constructed:
public class LoadingPresenter implements LoadingContract.Presenter {
private SharedPreferences sharedPreferences;
private LoadingContract.View loadingView;
@Inject
public LoadingPresenter(LoadingContract.View loadingView, SharedPreferences sharedPreferences) {
this.loadingView = loadingView;
this.sharedPreferences = sharedPreferences;
}
@Inject
void setupListeners() {
loadingView.setPresenter(this);
}
public boolean checkLoginStatus() {
SharedPreferences.Editor editor = sharedPreferences.edit();
return sharedPreferences.getBoolean("loginStatus", false);
}
@Override
public void start() {
}
}
End of my program.
It has been annoying me for several days. Any help is appreciated.