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

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

我已经实现的代码分别如下所示。我搜索了大多数类似的问题,但找不到解决方案,

在AppModule中,我要添加,

@ContributesAndroidInjector
  abstract PendingFragment pendingFragmentInjector();


Application类。

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


任何帮助将不胜感激 ,

谢谢。

最佳答案

解决了这个问题

活动类必须实现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;
      }
    }


希望它将来能对某人有所帮助。

10-08 07:12