9

Trying to learn Dagger2 and using Dagger2 2.14.1. I can't get past:

no suitable method found for inject(SettingsFragment)

SettingsFragment is:

public class SettingsFragment extends PreferenceFragmentCompat implements SharedPreferences.OnSharedPreferenceChangeListener {
....
 @Override
public void onAttach(Context context){
    AndroidInjection.inject(this);     <<<< Error here
    super.onAttach(context);
}}

as I can't extend PreferenceFragmentCompat from DaggerFragment.

My builders module is

@Module(subcomponents = {SettingsFragmentSubComponent.class, StartupPhotoFragmentSubComponent.class })
public abstract class UIBuildersModule {

@Binds
@IntoMap
@FragmentKey(SettingsFragment.class)
abstract AndroidInjector.Factory<? extends Fragment> bindSettingsFragmentInjectorFactory(SettingsFragmentSubComponent.Builder builder);

//    @ContributesAndroidInjector    <<< tried also without luck.
//    abstract SettingsFragment bindSettingsFragment();

@ContributesAndroidInjector
abstract StartupPhotoFragment bindStartupPhotoFragment();

I did create module and subcomponent for SettingsFragment which are really empty as I want to get dependencies that I created at app level:

@Module
public class SettingsFragmentModule {
}

@Subcomponent(modules = {SettingsFragmentModule.class})
public interface SettingsFragmentSubComponent extends 
AndroidInjector<SettingsFragment> {

@Subcomponent.Builder
abstract class Builder extends AndroidInjector.Builder<SettingsFragment> {
}}

Edit: Added ApplicationComponent that includes UIBuildersModule, and snippet from build.gradle

@Singleton
@Component(modules = {UIBuildersModule.class, 
AndroidSupportInjectionModule.class, ApplicationModule.class, })
public interface ApplicationComponent extends
    AndroidInjector<ActivityTrackerApplication> {

@Component.Builder
interface Builder {
    @BindsInstance
    Builder application(ActivityTrackerApplication application);
    ApplicationComponent build();
}
void inject(ActivityTrackerApplication activityTrackerApplication);
void inject(StartupPhotoFragment startupPhotoFragment);
void inject(SettingsFragment settingsFragment);

}

Also here is snippet from build.gradle

final DAGGER_VERSION="2.14.1"
 ....
// For Dagger2
compile "com.google.dagger:dagger:$DAGGER_VERSION"
compile "com.google.dagger:dagger-android:$DAGGER_VERSION"
compile "com.google.dagger:dagger-android-support:$DAGGER_VERSION"
annotationProcessor "com.google.dagger:dagger-android-processor:$DAGGER_VERSION"
annotationProcessor "com.google.dagger:dagger-compiler:$DAGGER_VERSION"

EDIT2: Solution Ahh - but of course.

public class SettingsFragment extends PreferenceFragmentCompat implements SharedPreferences.OnSharedPreferenceChangeListener {
....
 @Override
public void onAttach(Context context){
    AndroidSupportInjection.inject(this); <<<<Needed AndroidSupportInjection
    super.onAttach(context);
}}
Eric
  • 781
  • 1
  • 10
  • 18
  • You declare the injector factory in `UIBuildersModule`. You did not include any code that shows how or where you add `UIBuildersModule` to some other component. Are you adding it somewhere? You need to add it for Dagger to be able to inject your fragment. – David Medenjak Feb 07 '18 at 16:41
  • @DavidMedenjak - Sorry - Edited to show ApplicationComponent that include UIBuildersModule. Also build.gradle snippet for dagger libraries. – Eric Feb 08 '18 at 13:44
  • you are using a subclass AndroidInjector to you main component interface class ApplicationComponent; You should be using ApplicationComponent.inject(this); instead of AndroidInjector.inject(this); Just pure java bug i guess – Jileshl May 02 '19 at 12:39
  • What exact error you are getting? – Pravin Divraniya Mar 02 '20 at 13:07

0 Answers0