简体   繁体   English

未找到碎片匕首 2.11 的注入器

[英]No injector was found for fragment dagger 2.11

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.我正在尝试注入片段,但我收到“未找到 com.tsiro.dogvip.login.signin.SignInFrgmt 的注入器”异常。

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. Fragment 类扩展 BaseFragment,其中实现了 HasSupportFragmentInjector。

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).您的代码的问题是接口HasSupportFragmentInjectorHasFragmentInjector实现不正确(这取决于您是否使用片段的支持库)。

You should implement this interface either in your Activity that is hosting the fragment, or in your Application class.您应该在承载片段的 Activity 或 Application 类中实现此接口。 Personally, I 'd recommend the following Application class so you don't bother implementing it on every Activity that hosts a Fragment:就个人而言,我建议使用以下 Application 类,这样您就不必费心在每个承载 Fragment 的 Activity 上实现它:

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而不是手动定义组件,请确保您的绑定模块中有一个用于屏幕的条目:

@ContributesAndroidInjector(modules = MyScreenModule.class) abstract MyScreenActivity bindMyScreen();

If you still can't get it to work.如果你仍然无法让它工作。 I recommend reading the actual Dagger documentation :我建议阅读实际的Dagger 文档

Hope it helps.希望它有帮助。

In my case, My Fragment already extends DaggerFragment .就我而言, My Fragment已经扩展了DaggerFragment
However, my Activity extends AppCompatActivity and I got error No injector was found for fragment dagger但是,我的Activity扩展了AppCompatActivity并且出现错误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这很奇怪,因为错误是关于Fragment并且没有使用Fragment ,Activity 仍然可以正常工作

Change Activity extends DaggerAppCompatActivity make it work更改Activity扩展DaggerAppCompatActivity使其工作

I fixed this bug.我修复了这个错误。 First , extend your fragment with DaggerFragment首先,使用 DaggerFragment 扩展您的片段

class BaseFragment : DaggerFragment(){}

After, add the onAttach method in the same Fragment :之后,在同一个 Fragment 中添加 onAttach 方法:

  override fun onAttach(context: Context?) {
    AndroidSupportInjection.inject(this)
    super.onAttach(context)
}

PS: I'm using Kotlin. PS:我正在使用 Kotlin。

When I use dagger version 2.26 that problem happened, then I change dagger version to 2.23.2 ten it work.当我使用 dagger 版本2.26发生了这个问题,然后我将 dagger 版本更改为2.23.2十它工作。 Try to use 2.23.2 vesion:尝试使用 2.23.2 版本:

implementation "com.google.dagger:dagger-android-support:2.23.2"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM