i have an Activity with one fragment. I am trying to inject the fragment but i am getting 'No injector was found for com.tsiro.dogvip.login.signin.SignInFrgmt' exception.
ActivityModule:
@Module(includes = BaseActivityModule.class)
public abstract class LoginActivityModule {
@PerFragment
@ContributesAndroidInjector(modules = SignInFragmentModule.class)
abstract SignInFrgmt signInFrgmtInjector();
@Binds
@PerActivity
abstract Activity activity(LoginActivity loginActivity);
}
FragmentModule:
@Module(includes = BaseFragmentModule.class)
public abstract class SignInFragmentModule {
@Binds
@Named(BaseFragmentModule.FRAGMENT)
@PerFragment
abstract Fragment fragment(SignInFrgmt signInFrgmt);
}
Fragment class extends BaseFragment where HasSupportFragmentInjector is implemented.
BaseFragment:
public abstract class BaseFragment extends Fragment implements HasSupportFragmentInjector, Lifecycle.View {
@Inject
DispatchingAndroidInjector<Fragment> fragmentInjector;
public abstract Lifecycle.ViewModel getViewModel();
@SuppressWarnings("deprecation")
@Override
public void onAttach(Activity activity) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
// Perform injection here before M, L (API 22) and below because onAttach(Context)
// is not yet available at L.
AndroidSupportInjection.inject(this);
}
super.onAttach(activity);
}
@Override
public void onAttach(Context context) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
// Perform injection here for M (API 23) due to deprecation of onAttach(Activity).
AndroidSupportInjection.inject(this);
}
super.onAttach(context);
}
@Override
public void onStart() {
super.onStart();
getViewModel().onViewAttached(this);
}
@Override
public void onResume() {
super.onResume();
getViewModel().onViewResumed();
}
@Override
public void onPause() {
super.onPause();
}
@Override
public void onStop() {
super.onStop();
getViewModel().onViewDetached();
}
@Override
public AndroidInjector<Fragment> supportFragmentInjector() {
return fragmentInjector;
}
}
Can anybody tell me what i am missing?
The problem with your code, is the incorrect implementation of the interface HasSupportFragmentInjector
or HasFragmentInjector
(it depends on whether you are using the support library for fragments or not).
You should implement this interface either in your Activity that is hosting the fragment, or in your Application class. Personally, I 'd recommend the following Application class so you don't bother implementing it on every Activity that hosts a Fragment:
public class MyApplication extends Application implements HasActivityInjector, HasSupportFragmentInjector {
@Inject
DispatchingAndroidInjector<Activity> mActivityInjector;
@Inject
DispatchingAndroidInjector<Fragment> mFragmentInjector;
@Override
public void onCreate() {
super.onCreate();
//The way you build your top-level Application component can vary. This is just an example
DaggerApplicationComponent.builder()
.application(this)
.build()
.inject(this);
}
@Override
public AndroidInjector<Activity> activityInjector() {
return mActivityInjector;
}
@Override
public AndroidInjector<Fragment> supportFragmentInjector() {
return mFragmentInjector;
}
}
And then in you inject your Fragments (as you already do in your code):
@Override
public void onAttach(Context context) {
AndroidSupportInjection.inject(this);
super.onAttach(context);
}
If the above is not working, make sure you have created the appropriate Components/Subcomponents for the screen you are trying to inject. If you are using @ContributesAndroidInjector
instead of manually defining components, make sure you have one entry for your screen in your binding Module:
@ContributesAndroidInjector(modules = MyScreenModule.class) abstract MyScreenActivity bindMyScreen();
If you still can't get it to work. I recommend reading the actual Dagger documentation :
Hope it helps.
In my case, My Fragment
already extends DaggerFragment
.
However, my Activity
extends AppCompatActivity
and I got error No injector was found for fragment dagger
It quite strange because the error is mention about Fragment
and without using fragment, the Activity still work fine
Change Activity
extends DaggerAppCompatActivity
make it work
I fixed this bug. First , extend your fragment with DaggerFragment
class BaseFragment : DaggerFragment(){}
After, add the onAttach method in the same Fragment :
override fun onAttach(context: Context?) {
AndroidSupportInjection.inject(this)
super.onAttach(context)
}
PS: I'm using Kotlin.
When I use dagger version 2.26
that problem happened, then I change dagger version to 2.23.2
ten it work. Try to use 2.23.2 vesion:
implementation "com.google.dagger:dagger-android-support:2.23.2"
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.