簡體   English   中英

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

[英]No injector was found for fragment dagger 2.11

我有一個帶有一個片段的活動。 我正在嘗試注入片段,但我收到“未找到 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 類擴展 BaseFragment,其中實現了 HasSupportFragmentInjector。

基礎片段:

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;
}

 }

誰能告訴我我錯過了什么?

您的代碼的問題是接口HasSupportFragmentInjectorHasFragmentInjector實現不正確(這取決於您是否使用片段的支持庫)。

您應該在承載片段的 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 文檔

希望它有幫助。

就我而言, My Fragment已經擴展了DaggerFragment
但是,我的Activity擴展了AppCompatActivity並且出現錯誤No injector was found for fragment dagger

這很奇怪,因為錯誤是關於Fragment並且沒有使用Fragment ,Activity 仍然可以正常工作

更改Activity擴展DaggerAppCompatActivity使其工作

我修復了這個錯誤。 首先,使用 DaggerFragment 擴展您的片段

class BaseFragment : DaggerFragment(){}

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

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

PS:我正在使用 Kotlin。

當我使用 dagger 版本2.26發生了這個問題,然后我將 dagger 版本更改為2.23.2十它工作。 嘗試使用 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