没有发现碎片匕首2.14.1的注射器

时间:2018-01-16 10:05:24

标签: android android-fragments dagger-2

在尝试注入片段时,应用程序因抛出错误而崩溃

java.lang.IllegalArgumentException:未找到片段的注射器

我已经实施的代码分别在下面给出。我搜索了大部分类似的问题,但未能找到解决方案,

在AppModule中我正在添加,

@ContributesAndroidInjector
  abstract PendingFragment pendingFragmentInjector();

应用程序类。

public class Application extends Application implements HasActivityInjector,HasFragmentInjector{

  @Inject
  DispatchingAndroidInjector<Activity> androidInjector;
  @Inject
  DispatchingAndroidInjector<Fragment> mFragmentInjector;
  private String androidId;

  @Override
  public void onCreate() {
    super.onCreate();
    if (BuildConfig.DEBUG) {
      Timber.plant(new Timber.DebugTree());
    }
    Stetho.initializeWithDefaults(this);
    DaggerAppComponent.builder().create(this).inject(this);
    //androidId = Settings.Secure.getString(getContentResolver(), Settings.Secure.ANDROID_ID);

  }

  public String getAndroidId() {
    return androidId;
  }

  @Override
  public AndroidInjector<Activity> activityInjector() {
    return androidInjector;
  }

  @Override
  public AndroidInjector<Fragment> fragmentInjector() {
    return mFragmentInjector;
  }
}

的活动:

public class MyRequestActivity extends AppCompatActivity implements HasActivityInjector,
      HasFragmentInjector {

      @Inject
      DispatchingAndroidInjector<Activity> mDispatchingAndroidInjector;
      @Inject
      DispatchingAndroidInjector<Fragment> mFragmentInjector;
      @Inject
      ViewModelProvider.Factory factory;
    @Override
      protected void onCreate(Bundle savedInstanceState) {
        AndroidInjection.inject(this);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_request);
        ButterKnife.bind(this);
        myRequestsViewModel = ViewModelProviders.of(this, factory).get(MyRequestsViewModel.class);
        dataPassingviewModel = ViewModelProviders.of(this).get(DataPassingViewModel.class);

      }
    @Override
      public AndroidInjector<Fragment> fragmentInjector() {
        return mFragmentInjector;
      }

      @Override
      public AndroidInjector<Activity> activityInjector() {
        return mDispatchingAndroidInjector;
      }
    }

片段:

public class PendingFragment extends Fragment implements HasSupportFragmentInjector {

  @BindView(R.id.rvPending)
  RecyclerView rvPending;
  Unbinder unbinder;
  private Context mContext;

  private DataPassingViewModel dataPassingViewModel;

  @Inject
  DispatchingAndroidInjector<Fragment> mDispatchingAndroidInjector;
  @Inject
  ViewModelProvider.Factory mFactory;

  //public static PendingFragment newInstance() {
  //  return new PendingFragment();
  //}

  PendingRecyclerViewAdapter adapter;

  @Nullable
  @Override
  public View onCreateView(
    @NonNull LayoutInflater inflater,
    @Nullable ViewGroup container,
    @Nullable Bundle savedInstanceState
  ) {
    final View view = inflater.inflate(R.layout.fragment_pending, container, false);
    unbinder = ButterKnife.bind(this, view);
    return view;
  }

  @Override
  public void onSaveInstanceState(@NonNull Bundle outState) {
    super.onSaveInstanceState(outState);
    if (adapter != null) {
      adapter.saveStates(outState);
    }
  }

  @Override
  public void onViewStateRestored(@Nullable Bundle savedInstanceState) {
    super.onViewStateRestored(savedInstanceState);
    if (adapter != null) {
      adapter.restoreStates(savedInstanceState);
    }
  }

  public void update(final List<ServicePending> pendingList) {
    rvPending.setAdapter(new PendingRecyclerViewAdapter(pendingList));
  }

  @Override
  public void onDestroyView() {
    super.onDestroyView();
    unbinder.unbind();
  }

  @Override
  public AndroidInjector<Fragment> supportFragmentInjector() {
    return mDispatchingAndroidInjector;
  }

  private void observeOn() {
    dataPassingViewModel.getObservable().observe(this, passedObject -> {
      if (passedObject instanceof GetServiceResponse) {

        final GetServiceResponse pendingList = (GetServiceResponse) passedObject;
        if (pendingList.pending != null && pendingList.pending.size() > 0) {
          update(pendingList.pending);
        }
      }
    });
  }

  @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);
    }
    this.mContext = context;
    rvPending.setLayoutManager(new LinearLayoutManager(getContext()));
    rvPending.setNestedScrollingEnabled(true);
    if (getActivity() != null) {
      dataPassingViewModel = ViewModelProviders.of(getActivity()).get(DataPassingViewModel.class);
    }
    observeOn();
    super.onAttach(context);
  }

  @SuppressWarnings("deprecation")
  @Override
  public void onAttach(Activity activity) {
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
      // Perform injection here for versions before M as onAttach(*Context*) did not yet exist
      AndroidSupportInjection.inject(this);
    }
    this.mContext = activity;
    rvPending.setLayoutManager(new LinearLayoutManager(getContext()));
    rvPending.setNestedScrollingEnabled(true);
    if (getActivity() != null) {
      dataPassingViewModel = ViewModelProviders.of(getActivity()).get(DataPassingViewModel.class);
    }
    observeOn();
    super.onAttach(activity);
  }
}

任何帮助将不胜感激,

谢谢。

2 个答案:

答案 0 :(得分:3)

解决了这个问题,

活动类必须实现 HasSupportFragmentInjector 而不是 HasFragmentInjector

替换

 @Inject
  DispatchingAndroidInjector<Fragment> mFragmentInjector;

 @Inject
  DispatchingAndroidInjector<android.support.v4.app.Fragment> mFragmentDispatchingAndroidInjector;

例如:

public class MyRequestActivity extends AppCompatActivity implements HasActivityInjector,
      HasFragmentInjector {

      @Inject
      DispatchingAndroidInjector<Activity> mDispatchingAndroidInjector;
     @Inject
  DispatchingAndroidInjector<android.support.v4.app.Fragment> mFragmentDispatchingAndroidInjector;
      @Inject
      ViewModelProvider.Factory factory;
    @Override
      protected void onCreate(Bundle savedInstanceState) {
        AndroidInjection.inject(this);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_request);
        ButterKnife.bind(this);
        myRequestsViewModel = ViewModelProviders.of(this, factory).get(MyRequestsViewModel.class);
        dataPassingviewModel = ViewModelProviders.of(this).get(DataPassingViewModel.class);

      }
      @Override
  public AndroidInjector<android.support.v4.app.Fragment> supportFragmentInjector() {
    return mFragmentDispatchingAndroidInjector;
  }


      @Override
      public AndroidInjector<Activity> activityInjector() {
        return mDispatchingAndroidInjector;
      }
    }

希望将来可以帮助某人。

答案 1 :(得分:0)

使您的活动扩展DaggerAppCompatActivity。这将为您提供开箱即用的所有功能。