【问题标题】:No injector was found for fragment dagger 2.11没有找到 fragment dagger 2.11 的注入器
【发布时间】:2018-03-31 17:58:13
【问题描述】:

我有一个带有一个片段的活动。我正在尝试注入片段,但我收到“没有为 com.tsiro.dogvip.login.signin.SignInFrgmt 找到注入器”异常。

活动模块:

@Module(includes = BaseActivityModule.class)
public abstract class LoginActivityModule {

   @PerFragment
   @ContributesAndroidInjector(modules = SignInFragmentModule.class)
   abstract SignInFrgmt signInFrgmtInjector();

   @Binds
   @PerActivity
   abstract Activity activity(LoginActivity loginActivity);
}

片段模块:

@Module(includes = BaseFragmentModule.class)
public abstract class SignInFragmentModule {

@Binds
@Named(BaseFragmentModule.FRAGMENT)
@PerFragment
abstract Fragment fragment(SignInFrgmt signInFrgmt);

}

Fragment 类扩展了实现 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;
}

 }

谁能告诉我我错过了什么?

【问题讨论】:

  • 上述代码中你的fragment是什么时候注入的?

标签: android


【解决方案1】:

您的代码的问题是接口HasSupportFragmentInjector 或HasFragmentInjector 的错误实现(这取决于您是否使用片段的支持库)。

您应该在托管片段的 Activity 或 Application 类中实现此接口。就个人而言,我推荐以下 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;
    }
}

然后你注入你的片段(就像你已经在你的代码中所做的那样):

@Override
public void onAttach(Context context) {
    AndroidSupportInjection.inject(this);
    super.onAttach(context);
}

如果上述方法不起作用,请确保您已为您尝试注入的屏幕创建了适当的组件/子组件。如果您使用@ContributesAndroidInjector 而不是手动定义组件,请确保您的绑定模块中有一个屏幕条目:

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

如果你仍然无法让它工作。我建议阅读实际的Dagger documentation:

希望对你有帮助。

【讨论】:

  • 对我不起作用,我的错误信息略有不同:java.lang.IllegalArgumentException: No injector factory bound for Class&lt;...MyFragment&gt;。 :( 我的Application 子类中已经有supportFragmentInjector()
  • 我也面临同样的问题。希望尽快解决并发布解决方案。
  • 别忘了将 MyApplication 添加到 AndroidMnifest,这是我的操作。
  • 别忘了将 MyApplication 添加到 AndroidMnifest,这是我的操作。
【解决方案2】:

就我而言, 我的Fragment 已经扩展了DaggerFragment。
但是,我的Activity 扩展了AppCompatActivity,我得到了错误 No injector was found for fragment dagger

这很奇怪,因为错误提到了Fragment,并且在不使用片段的情况下,Activity 仍然可以正常工作

更改 Activity 扩展 DaggerAppCompatActivity 使其工作

【讨论】:

  • 这拯救了我的一天!
  • 我收到这个错误:没有注入器工厂绑定到 Class xxx
  • 通过添加ActivityModule修复,请参考这个答案:stackoverflow.com/a/45327464/3260008
  • 我使用的是匕首 2.21
【解决方案3】:

我修复了这个错误。首先,用 DaggerFragment 扩展你的片段

class BaseFragment : DaggerFragment(){}

之后,在同一个 Fragment 中添加 onAttach 方法:

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

PS:我正在使用 Kotlin。

【讨论】:

    【解决方案4】:

    当我使用匕首版本2.26 时发生了这个问题,然后我将匕首版本更改为2.23.2 十它可以工作。尝试使用 2.23.2 版本:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-10-07
      • 2018-03-09
      • 2021-02-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-06
      • 1970-01-01
      相关资源
      最近更新 更多